File Index
Files and directories
Sign in
FILE

lpfc_attr.c

File preview with syntax highlighting.

linux-6.19.14/drivers/scsi/lpfc/lpfc_attr.c C
   1/*******************************************************************
   2 * This file is part of the Emulex Linux Device Driver for         *
   3 * Fibre Channel Host Bus Adapters.                                *
   4 * Copyright (C) 2017-2025 Broadcom. All Rights Reserved. The term *
   5 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.  *
   6 * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
   7 * EMULEX and SLI are trademarks of Emulex.                        *
   8 * www.broadcom.com                                                *
   9 * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
  10 *                                                                 *
  11 * This program is free software; you can redistribute it and/or   *
  12 * modify it under the terms of version 2 of the GNU General       *
  13 * Public License as published by the Free Software Foundation.    *
  14 * This program is distributed in the hope that it will be useful. *
  15 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
  16 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
  17 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
  18 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  19 * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
  20 * more details, a copy of which can be found in the file COPYING  *
  21 * included with this package.                                     *
  22 *******************************************************************/
  23
  24#include <linux/ctype.h>
  25#include <linux/delay.h>
  26#include <linux/pci.h>
  27#include <linux/interrupt.h>
  28#include <linux/module.h>
  29#include <linux/aer.h>
  30#include <linux/gfp.h>
  31#include <linux/kernel.h>
  32
  33#include <scsi/scsi.h>
  34#include <scsi/scsi_device.h>
  35#include <scsi/scsi_host.h>
  36#include <scsi/scsi_tcq.h>
  37#include <scsi/scsi_transport_fc.h>
  38#include <scsi/fc/fc_fs.h>
  39
  40#include "lpfc_hw4.h"
  41#include "lpfc_hw.h"
  42#include "lpfc_sli.h"
  43#include "lpfc_sli4.h"
  44#include "lpfc_nl.h"
  45#include "lpfc_disc.h"
  46#include "lpfc.h"
  47#include "lpfc_scsi.h"
  48#include "lpfc_nvme.h"
  49#include "lpfc_logmsg.h"
  50#include "lpfc_version.h"
  51#include "lpfc_compat.h"
  52#include "lpfc_crtn.h"
  53#include "lpfc_vport.h"
  54#include "lpfc_attr.h"
  55
  56#define LPFC_DEF_DEVLOSS_TMO	30
  57#define LPFC_MIN_DEVLOSS_TMO	1
  58#define LPFC_MAX_DEVLOSS_TMO	255
  59
  60#define LPFC_MAX_INFO_TMP_LEN	100
  61#define LPFC_INFO_MORE_STR	"\nCould be more info...\n"
  62/*
  63 * Write key size should be multiple of 4. If write key is changed
  64 * make sure that library write key is also changed.
  65 */
  66#define LPFC_REG_WRITE_KEY_SIZE	4
  67#define LPFC_REG_WRITE_KEY	"EMLX"
  68
  69const char *const trunk_errmsg[] = {	/* map errcode */
  70	"",	/* There is no such error code at index 0*/
  71	"link negotiated speed does not match existing"
  72		" trunk - link was \"low\" speed",
  73	"link negotiated speed does not match"
  74		" existing trunk - link was \"middle\" speed",
  75	"link negotiated speed does not match existing"
  76		" trunk - link was \"high\" speed",
  77	"Attached to non-trunking port - F_Port",
  78	"Attached to non-trunking port - N_Port",
  79	"FLOGI response timeout",
  80	"non-FLOGI frame received",
  81	"Invalid FLOGI response",
  82	"Trunking initialization protocol",
  83	"Trunk peer device mismatch",
  84};
  85
  86/**
  87 * lpfc_jedec_to_ascii - Hex to ascii convertor according to JEDEC rules
  88 * @incr: integer to convert.
  89 * @hdw: ascii string holding converted integer plus a string terminator.
  90 *
  91 * Description:
  92 * JEDEC Joint Electron Device Engineering Council.
  93 * Convert a 32 bit integer composed of 8 nibbles into an 8 byte ascii
  94 * character string. The string is then terminated with a NULL in byte 9.
  95 * Hex 0-9 becomes ascii '0' to '9'.
  96 * Hex a-f becomes ascii '=' to 'B' capital B.
  97 *
  98 * Notes:
  99 * Coded for 32 bit integers only.
 100 **/
 101static void
 102lpfc_jedec_to_ascii(int incr, char hdw[])
 103{
 104	int i, j;
 105	for (i = 0; i < 8; i++) {
 106		j = (incr & 0xf);
 107		if (j <= 9)
 108			hdw[7 - i] = 0x30 +  j;
 109		 else
 110			hdw[7 - i] = 0x61 + j - 10;
 111		incr = (incr >> 4);
 112	}
 113	hdw[8] = 0;
 114	return;
 115}
 116
 117static ssize_t
 118lpfc_cmf_info_show(struct device *dev, struct device_attribute *attr,
 119		   char *buf)
 120{
 121	struct Scsi_Host  *shost = class_to_shost(dev);
 122	struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
 123	struct lpfc_hba   *phba = vport->phba;
 124	struct lpfc_cgn_info *cp = NULL;
 125	struct lpfc_cgn_stat *cgs;
 126	int  len = 0;
 127	int cpu;
 128	u64 rcv, total;
 129	char tmp[LPFC_MAX_INFO_TMP_LEN] = {0};
 130
 131	if (phba->cgn_i)
 132		cp = (struct lpfc_cgn_info *)phba->cgn_i->virt;
 133
 134	scnprintf(tmp, sizeof(tmp),
 135		  "Congestion Mgmt Info: E2Eattr %d Ver %d "
 136		  "CMF %d cnt %d\n",
 137		  phba->sli4_hba.pc_sli4_params.mi_cap,
 138		  cp ? cp->cgn_info_version : 0,
 139		  phba->sli4_hba.pc_sli4_params.cmf, phba->cmf_timer_cnt);
 140
 141	if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 142		goto buffer_done;
 143
 144	if (!phba->sli4_hba.pc_sli4_params.cmf)
 145		goto buffer_done;
 146
 147	switch (phba->cgn_init_reg_signal) {
 148	case EDC_CG_SIG_WARN_ONLY:
 149		scnprintf(tmp, sizeof(tmp),
 150			  "Register: Init:  Signal:WARN  ");
 151		break;
 152	case EDC_CG_SIG_WARN_ALARM:
 153		scnprintf(tmp, sizeof(tmp),
 154			  "Register: Init:  Signal:WARN|ALARM  ");
 155		break;
 156	default:
 157		scnprintf(tmp, sizeof(tmp),
 158			  "Register: Init:  Signal:NONE  ");
 159		break;
 160	}
 161	if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 162		goto buffer_done;
 163
 164	switch (phba->cgn_init_reg_fpin) {
 165	case LPFC_CGN_FPIN_WARN:
 166		scnprintf(tmp, sizeof(tmp),
 167			  "FPIN:WARN\n");
 168		break;
 169	case LPFC_CGN_FPIN_ALARM:
 170		scnprintf(tmp, sizeof(tmp),
 171			  "FPIN:ALARM\n");
 172		break;
 173	case LPFC_CGN_FPIN_BOTH:
 174		scnprintf(tmp, sizeof(tmp),
 175			  "FPIN:WARN|ALARM\n");
 176		break;
 177	default:
 178		scnprintf(tmp, sizeof(tmp),
 179			  "FPIN:NONE\n");
 180		break;
 181	}
 182	if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 183		goto buffer_done;
 184
 185	switch (phba->cgn_reg_signal) {
 186	case EDC_CG_SIG_WARN_ONLY:
 187		scnprintf(tmp, sizeof(tmp),
 188			  "       Current:  Signal:WARN  ");
 189		break;
 190	case EDC_CG_SIG_WARN_ALARM:
 191		scnprintf(tmp, sizeof(tmp),
 192			  "       Current:  Signal:WARN|ALARM  ");
 193		break;
 194	default:
 195		scnprintf(tmp, sizeof(tmp),
 196			  "       Current:  Signal:NONE  ");
 197		break;
 198	}
 199	if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 200		goto buffer_done;
 201
 202	switch (phba->cgn_reg_fpin) {
 203	case LPFC_CGN_FPIN_WARN:
 204		scnprintf(tmp, sizeof(tmp),
 205			  "FPIN:WARN  ACQEcnt:%d\n", phba->cgn_acqe_cnt);
 206		break;
 207	case LPFC_CGN_FPIN_ALARM:
 208		scnprintf(tmp, sizeof(tmp),
 209			  "FPIN:ALARM  ACQEcnt:%d\n", phba->cgn_acqe_cnt);
 210		break;
 211	case LPFC_CGN_FPIN_BOTH:
 212		scnprintf(tmp, sizeof(tmp),
 213			  "FPIN:WARN|ALARM  ACQEcnt:%d\n", phba->cgn_acqe_cnt);
 214		break;
 215	default:
 216		scnprintf(tmp, sizeof(tmp),
 217			  "FPIN:NONE  ACQEcnt:%d\n", phba->cgn_acqe_cnt);
 218		break;
 219	}
 220	if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 221		goto buffer_done;
 222
 223	if (phba->cmf_active_mode != phba->cgn_p.cgn_param_mode) {
 224		switch (phba->cmf_active_mode) {
 225		case LPFC_CFG_OFF:
 226			scnprintf(tmp, sizeof(tmp), "Active: Mode:Off\n");
 227			break;
 228		case LPFC_CFG_MANAGED:
 229			scnprintf(tmp, sizeof(tmp), "Active: Mode:Managed\n");
 230			break;
 231		case LPFC_CFG_MONITOR:
 232			scnprintf(tmp, sizeof(tmp), "Active: Mode:Monitor\n");
 233			break;
 234		default:
 235			scnprintf(tmp, sizeof(tmp), "Active: Mode:Unknown\n");
 236		}
 237		if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 238			goto buffer_done;
 239	}
 240
 241	switch (phba->cgn_p.cgn_param_mode) {
 242	case LPFC_CFG_OFF:
 243		scnprintf(tmp, sizeof(tmp), "Config: Mode:Off  ");
 244		break;
 245	case LPFC_CFG_MANAGED:
 246		scnprintf(tmp, sizeof(tmp), "Config: Mode:Managed ");
 247		break;
 248	case LPFC_CFG_MONITOR:
 249		scnprintf(tmp, sizeof(tmp), "Config: Mode:Monitor ");
 250		break;
 251	default:
 252		scnprintf(tmp, sizeof(tmp), "Config: Mode:Unknown ");
 253	}
 254	if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 255		goto buffer_done;
 256
 257	total = 0;
 258	rcv = 0;
 259	for_each_present_cpu(cpu) {
 260		cgs = per_cpu_ptr(phba->cmf_stat, cpu);
 261		total += atomic64_read(&cgs->total_bytes);
 262		rcv += atomic64_read(&cgs->rcv_bytes);
 263	}
 264
 265	scnprintf(tmp, sizeof(tmp),
 266		  "IObusy:%d Info:%d Bytes: Rcv:x%llx Total:x%llx\n",
 267		  atomic_read(&phba->cmf_busy),
 268		  phba->cmf_active_info, rcv, total);
 269	if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 270		goto buffer_done;
 271
 272	scnprintf(tmp, sizeof(tmp),
 273		  "Port_speed:%d  Link_byte_cnt:%ld  "
 274		  "Max_byte_per_interval:%ld\n",
 275		  lpfc_sli_port_speed_get(phba),
 276		  (unsigned long)phba->cmf_link_byte_count,
 277		  (unsigned long)phba->cmf_max_bytes_per_interval);
 278	strlcat(buf, tmp, PAGE_SIZE);
 279
 280buffer_done:
 281	len = strnlen(buf, PAGE_SIZE);
 282
 283	if (unlikely(len >= (PAGE_SIZE - 1))) {
 284		lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
 285				"6312 Catching potential buffer "
 286				"overflow > PAGE_SIZE = %lu bytes\n",
 287				PAGE_SIZE);
 288		strscpy(buf + PAGE_SIZE - 1 - sizeof(LPFC_INFO_MORE_STR),
 289			LPFC_INFO_MORE_STR, sizeof(LPFC_INFO_MORE_STR) + 1);
 290	}
 291	return len;
 292}
 293
 294static ssize_t
 295lpfc_vmid_info_show(struct device *dev, struct device_attribute *attr,
 296		    char *buf)
 297{
 298	struct Scsi_Host  *shost = class_to_shost(dev);
 299	struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
 300	struct lpfc_hba   *phba = vport->phba;
 301	struct lpfc_vmid  *vmp;
 302	int  len = 0, i, j, k, cpu;
 303	char hxstr[LPFC_MAX_VMID_SIZE * 3] = {0};
 304	struct timespec64 curr_tm;
 305	struct lpfc_vmid_priority_range *vr;
 306	u64 *lta, rct_acc = 0, max_lta = 0;
 307	struct tm tm_val;
 308
 309	ktime_get_ts64(&curr_tm);
 310
 311	len += scnprintf(buf + len, PAGE_SIZE - len, "Key 'vmid':\n");
 312
 313	/* if enabled continue, else return */
 314	if (lpfc_is_vmid_enabled(phba)) {
 315		len += scnprintf(buf + len, PAGE_SIZE - len,
 316				 "lpfc VMID Page: ON\n\n");
 317	} else {
 318		len += scnprintf(buf + len, PAGE_SIZE - len,
 319				 "lpfc VMID Page: OFF\n\n");
 320		return len;
 321	}
 322
 323	/* if using priority tagging */
 324	if (vport->phba->pport->vmid_flag & LPFC_VMID_TYPE_PRIO) {
 325		len += scnprintf(buf + len, PAGE_SIZE - len,
 326				"VMID priority ranges:\n");
 327		vr = vport->vmid_priority.vmid_range;
 328		for (i = 0; i < vport->vmid_priority.num_descriptors; ++i) {
 329			len += scnprintf(buf + len, PAGE_SIZE - len,
 330					"\t[x%x - x%x], qos: x%x\n",
 331					vr->low, vr->high, vr->qos);
 332			vr++;
 333		}
 334	}
 335
 336	for (i = 0; i < phba->cfg_max_vmid; i++) {
 337		vmp = &vport->vmid[i];
 338		max_lta = 0;
 339
 340		/* only if the slot is used */
 341		if (!(vmp->flag & LPFC_VMID_SLOT_USED) ||
 342		    !(vmp->flag & LPFC_VMID_REGISTERED))
 343			continue;
 344
 345		/* if using priority tagging */
 346		if (vport->phba->pport->vmid_flag & LPFC_VMID_TYPE_PRIO) {
 347			len += scnprintf(buf + len, PAGE_SIZE - len,
 348					"VEM ID: %02x:%02x:%02x:%02x:"
 349					"%02x:%02x:%02x:%02x:%02x:%02x:"
 350					"%02x:%02x:%02x:%02x:%02x:%02x\n",
 351					vport->lpfc_vmid_host_uuid[0],
 352					vport->lpfc_vmid_host_uuid[1],
 353					vport->lpfc_vmid_host_uuid[2],
 354					vport->lpfc_vmid_host_uuid[3],
 355					vport->lpfc_vmid_host_uuid[4],
 356					vport->lpfc_vmid_host_uuid[5],
 357					vport->lpfc_vmid_host_uuid[6],
 358					vport->lpfc_vmid_host_uuid[7],
 359					vport->lpfc_vmid_host_uuid[8],
 360					vport->lpfc_vmid_host_uuid[9],
 361					vport->lpfc_vmid_host_uuid[10],
 362					vport->lpfc_vmid_host_uuid[11],
 363					vport->lpfc_vmid_host_uuid[12],
 364					vport->lpfc_vmid_host_uuid[13],
 365					vport->lpfc_vmid_host_uuid[14],
 366					vport->lpfc_vmid_host_uuid[15]);
 367		}
 368
 369		/* IO stats */
 370		len += scnprintf(buf + len, PAGE_SIZE - len,
 371				"ID00 READs:%llx WRITEs:%llx\n",
 372				vmp->io_rd_cnt,
 373				vmp->io_wr_cnt);
 374		for (j = 0, k = 0; j < strlen(vmp->host_vmid); j++, k += 3)
 375			sprintf((char *)(hxstr + k), "%2x ", vmp->host_vmid[j]);
 376		/* UUIDs */
 377		len += scnprintf(buf + len, PAGE_SIZE - len, "UUID:\n");
 378		len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n", hxstr);
 379
 380		len += scnprintf(buf + len, PAGE_SIZE - len, "String (%s)\n",
 381				vmp->host_vmid);
 382
 383		if (vport->phba->pport->vmid_flag & LPFC_VMID_TYPE_PRIO)
 384			len += scnprintf(buf + len, PAGE_SIZE - len,
 385					"CS_CTL VMID: 0x%x\n",
 386					vmp->un.cs_ctl_vmid);
 387		else
 388			len += scnprintf(buf + len, PAGE_SIZE - len,
 389					"Application id: 0x%x\n",
 390					vmp->un.app_id);
 391
 392		/* calculate the last access time */
 393		for_each_possible_cpu(cpu) {
 394			lta = per_cpu_ptr(vmp->last_io_time, cpu);
 395			if (!lta)
 396				continue;
 397
 398			/* if last access time is less than timeout */
 399			if (time_after((unsigned long)*lta, jiffies))
 400				continue;
 401
 402			if (*lta > max_lta)
 403				max_lta = *lta;
 404		}
 405
 406		rct_acc = jiffies_to_msecs(jiffies - max_lta) / 1000;
 407		/* current time */
 408		time64_to_tm(ktime_get_real_seconds(),
 409			     -(sys_tz.tz_minuteswest * 60) - rct_acc, &tm_val);
 410
 411		len += scnprintf(buf + len, PAGE_SIZE - len,
 412				 "Last Access Time :"
 413				 "%ld-%d-%dT%02d:%02d:%02d\n\n",
 414				 1900 + tm_val.tm_year, tm_val.tm_mon + 1,
 415				 tm_val.tm_mday, tm_val.tm_hour,
 416				 tm_val.tm_min, tm_val.tm_sec);
 417
 418		if (len >= PAGE_SIZE)
 419			return len;
 420
 421		memset(hxstr, 0, LPFC_MAX_VMID_SIZE * 3);
 422	}
 423	return len;
 424}
 425
 426/**
 427 * lpfc_drvr_version_show - Return the Emulex driver string with version number
 428 * @dev: class unused variable.
 429 * @attr: device attribute, not used.
 430 * @buf: on return contains the module description text.
 431 *
 432 * Returns: size of formatted string.
 433 **/
 434static ssize_t
 435lpfc_drvr_version_show(struct device *dev, struct device_attribute *attr,
 436		       char *buf)
 437{
 438	return scnprintf(buf, PAGE_SIZE, LPFC_MODULE_DESC "\n");
 439}
 440
 441/**
 442 * lpfc_enable_fip_show - Return the fip mode of the HBA
 443 * @dev: class unused variable.
 444 * @attr: device attribute, not used.
 445 * @buf: on return contains the module description text.
 446 *
 447 * Returns: size of formatted string.
 448 **/
 449static ssize_t
 450lpfc_enable_fip_show(struct device *dev, struct device_attribute *attr,
 451		       char *buf)
 452{
 453	struct Scsi_Host *shost = class_to_shost(dev);
 454	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
 455	struct lpfc_hba   *phba = vport->phba;
 456
 457	if (test_bit(HBA_FIP_SUPPORT, &phba->hba_flag))
 458		return scnprintf(buf, PAGE_SIZE, "1\n");
 459	else
 460		return scnprintf(buf, PAGE_SIZE, "0\n");
 461}
 462
 463static ssize_t
 464lpfc_nvme_info_show(struct device *dev, struct device_attribute *attr,
 465		    char *buf)
 466{
 467	struct Scsi_Host *shost = class_to_shost(dev);
 468	struct lpfc_vport *vport = shost_priv(shost);
 469	struct lpfc_hba   *phba = vport->phba;
 470	struct lpfc_nvmet_tgtport *tgtp;
 471	struct nvme_fc_local_port *localport;
 472	struct lpfc_nvme_lport *lport;
 473	struct lpfc_nvme_rport *rport;
 474	struct lpfc_nodelist *ndlp;
 475	struct nvme_fc_remote_port *nrport;
 476	struct lpfc_fc4_ctrl_stat *cstat;
 477	uint64_t data1, data2, data3;
 478	uint64_t totin, totout, tot;
 479	unsigned long iflags;
 480	char *statep;
 481	int i;
 482	int len = 0;
 483	char tmp[LPFC_MAX_INFO_TMP_LEN] = {0};
 484
 485	if (!(vport->cfg_enable_fc4_type & LPFC_ENABLE_NVME)) {
 486		len = scnprintf(buf, PAGE_SIZE, "NVME Disabled\n");
 487		return len;
 488	}
 489	if (phba->nvmet_support) {
 490		if (!phba->targetport) {
 491			len = scnprintf(buf, PAGE_SIZE,
 492					"NVME Target: x%llx is not allocated\n",
 493					wwn_to_u64(vport->fc_portname.u.wwn));
 494			return len;
 495		}
 496		/* Port state is only one of two values for now. */
 497		if (phba->targetport->port_id)
 498			statep = "REGISTERED";
 499		else
 500			statep = "INIT";
 501		scnprintf(tmp, sizeof(tmp),
 502			  "NVME Target Enabled  State %s\n",
 503			  statep);
 504		if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 505			goto buffer_done;
 506
 507		scnprintf(tmp, sizeof(tmp),
 508			  "%s%d WWPN x%llx WWNN x%llx DID x%06x\n",
 509			  "NVME Target: lpfc",
 510			  phba->brd_no,
 511			  wwn_to_u64(vport->fc_portname.u.wwn),
 512			  wwn_to_u64(vport->fc_nodename.u.wwn),
 513			  phba->targetport->port_id);
 514		if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 515			goto buffer_done;
 516
 517		if (strlcat(buf, "\nNVME Target: Statistics\n", PAGE_SIZE)
 518		    >= PAGE_SIZE)
 519			goto buffer_done;
 520
 521		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
 522		scnprintf(tmp, sizeof(tmp),
 523			  "LS: Rcv %08x Drop %08x Abort %08x\n",
 524			  atomic_read(&tgtp->rcv_ls_req_in),
 525			  atomic_read(&tgtp->rcv_ls_req_drop),
 526			  atomic_read(&tgtp->xmt_ls_abort));
 527		if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 528			goto buffer_done;
 529
 530		if (atomic_read(&tgtp->rcv_ls_req_in) !=
 531		    atomic_read(&tgtp->rcv_ls_req_out)) {
 532			scnprintf(tmp, sizeof(tmp),
 533				  "Rcv LS: in %08x != out %08x\n",
 534				  atomic_read(&tgtp->rcv_ls_req_in),
 535				  atomic_read(&tgtp->rcv_ls_req_out));
 536			if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 537				goto buffer_done;
 538		}
 539
 540		scnprintf(tmp, sizeof(tmp),
 541			  "LS: Xmt %08x Drop %08x Cmpl %08x\n",
 542			  atomic_read(&tgtp->xmt_ls_rsp),
 543			  atomic_read(&tgtp->xmt_ls_drop),
 544			  atomic_read(&tgtp->xmt_ls_rsp_cmpl));
 545		if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 546			goto buffer_done;
 547
 548		scnprintf(tmp, sizeof(tmp),
 549			  "LS: RSP Abort %08x xb %08x Err %08x\n",
 550			  atomic_read(&tgtp->xmt_ls_rsp_aborted),
 551			  atomic_read(&tgtp->xmt_ls_rsp_xb_set),
 552			  atomic_read(&tgtp->xmt_ls_rsp_error));
 553		if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 554			goto buffer_done;
 555
 556		scnprintf(tmp, sizeof(tmp),
 557			  "FCP: Rcv %08x Defer %08x Release %08x "
 558			  "Drop %08x\n",
 559			  atomic_read(&tgtp->rcv_fcp_cmd_in),
 560			  atomic_read(&tgtp->rcv_fcp_cmd_defer),
 561			  atomic_read(&tgtp->xmt_fcp_release),
 562			  atomic_read(&tgtp->rcv_fcp_cmd_drop));
 563		if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 564			goto buffer_done;
 565
 566		if (atomic_read(&tgtp->rcv_fcp_cmd_in) !=
 567		    atomic_read(&tgtp->rcv_fcp_cmd_out)) {
 568			scnprintf(tmp, sizeof(tmp),
 569				  "Rcv FCP: in %08x != out %08x\n",
 570				  atomic_read(&tgtp->rcv_fcp_cmd_in),
 571				  atomic_read(&tgtp->rcv_fcp_cmd_out));
 572			if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 573				goto buffer_done;
 574		}
 575
 576		scnprintf(tmp, sizeof(tmp),
 577			  "FCP Rsp: RD %08x rsp %08x WR %08x rsp %08x "
 578			  "drop %08x\n",
 579			  atomic_read(&tgtp->xmt_fcp_read),
 580			  atomic_read(&tgtp->xmt_fcp_read_rsp),
 581			  atomic_read(&tgtp->xmt_fcp_write),
 582			  atomic_read(&tgtp->xmt_fcp_rsp),
 583			  atomic_read(&tgtp->xmt_fcp_drop));
 584		if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 585			goto buffer_done;
 586
 587		scnprintf(tmp, sizeof(tmp),
 588			  "FCP Rsp Cmpl: %08x err %08x drop %08x\n",
 589			  atomic_read(&tgtp->xmt_fcp_rsp_cmpl),
 590			  atomic_read(&tgtp->xmt_fcp_rsp_error),
 591			  atomic_read(&tgtp->xmt_fcp_rsp_drop));
 592		if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 593			goto buffer_done;
 594
 595		scnprintf(tmp, sizeof(tmp),
 596			  "FCP Rsp Abort: %08x xb %08x xricqe  %08x\n",
 597			  atomic_read(&tgtp->xmt_fcp_rsp_aborted),
 598			  atomic_read(&tgtp->xmt_fcp_rsp_xb_set),
 599			  atomic_read(&tgtp->xmt_fcp_xri_abort_cqe));
 600		if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 601			goto buffer_done;
 602
 603		scnprintf(tmp, sizeof(tmp),
 604			  "ABORT: Xmt %08x Cmpl %08x\n",
 605			  atomic_read(&tgtp->xmt_fcp_abort),
 606			  atomic_read(&tgtp->xmt_fcp_abort_cmpl));
 607		if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 608			goto buffer_done;
 609
 610		scnprintf(tmp, sizeof(tmp),
 611			  "ABORT: Sol %08x  Usol %08x Err %08x Cmpl %08x\n",
 612			  atomic_read(&tgtp->xmt_abort_sol),
 613			  atomic_read(&tgtp->xmt_abort_unsol),
 614			  atomic_read(&tgtp->xmt_abort_rsp),
 615			  atomic_read(&tgtp->xmt_abort_rsp_error));
 616		if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 617			goto buffer_done;
 618
 619		scnprintf(tmp, sizeof(tmp),
 620			  "DELAY: ctx %08x  fod %08x wqfull %08x\n",
 621			  atomic_read(&tgtp->defer_ctx),
 622			  atomic_read(&tgtp->defer_fod),
 623			  atomic_read(&tgtp->defer_wqfull));
 624		if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 625			goto buffer_done;
 626
 627		/* Calculate outstanding IOs */
 628		tot = atomic_read(&tgtp->rcv_fcp_cmd_drop);
 629		tot += atomic_read(&tgtp->xmt_fcp_release);
 630		tot = atomic_read(&tgtp->rcv_fcp_cmd_in) - tot;
 631
 632		scnprintf(tmp, sizeof(tmp),
 633			  "IO_CTX: %08x  WAIT: cur %08x tot %08x\n"
 634			  "CTX Outstanding %08llx\n\n",
 635			  phba->sli4_hba.nvmet_xri_cnt,
 636			  phba->sli4_hba.nvmet_io_wait_cnt,
 637			  phba->sli4_hba.nvmet_io_wait_total,
 638			  tot);
 639		strlcat(buf, tmp, PAGE_SIZE);
 640		goto buffer_done;
 641	}
 642
 643	localport = vport->localport;
 644	if (!localport) {
 645		len = scnprintf(buf, PAGE_SIZE,
 646				"NVME Initiator x%llx is not allocated\n",
 647				wwn_to_u64(vport->fc_portname.u.wwn));
 648		return len;
 649	}
 650	lport = (struct lpfc_nvme_lport *)localport->private;
 651	if (strlcat(buf, "\nNVME Initiator Enabled\n", PAGE_SIZE) >= PAGE_SIZE)
 652		goto buffer_done;
 653
 654	scnprintf(tmp, sizeof(tmp),
 655		  "XRI Dist lpfc%d Total %d IO %d ELS %d\n",
 656		  phba->brd_no,
 657		  phba->sli4_hba.max_cfg_param.max_xri,
 658		  phba->sli4_hba.io_xri_max,
 659		  lpfc_sli4_get_els_iocb_cnt(phba));
 660	if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 661		goto buffer_done;
 662
 663	/* Port state is only one of two values for now. */
 664	if (localport->port_id)
 665		statep = "ONLINE";
 666	else
 667		statep = "UNKNOWN ";
 668
 669	scnprintf(tmp, sizeof(tmp),
 670		  "%s%d WWPN x%llx WWNN x%llx DID x%06x %s\n",
 671		  "NVME LPORT lpfc",
 672		  phba->brd_no,
 673		  wwn_to_u64(vport->fc_portname.u.wwn),
 674		  wwn_to_u64(vport->fc_nodename.u.wwn),
 675		  localport->port_id, statep);
 676	if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 677		goto buffer_done;
 678
 679	spin_lock_irqsave(&vport->fc_nodes_list_lock, iflags);
 680
 681	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
 682		nrport = NULL;
 683		spin_lock(&ndlp->lock);
 684		rport = lpfc_ndlp_get_nrport(ndlp);
 685		if (rport)
 686			nrport = rport->remoteport;
 687		spin_unlock(&ndlp->lock);
 688		if (!nrport)
 689			continue;
 690
 691		/* Port state is only one of two values for now. */
 692		switch (nrport->port_state) {
 693		case FC_OBJSTATE_ONLINE:
 694			statep = "ONLINE";
 695			break;
 696		case FC_OBJSTATE_UNKNOWN:
 697			statep = "UNKNOWN ";
 698			break;
 699		default:
 700			statep = "UNSUPPORTED";
 701			break;
 702		}
 703
 704		/* Tab in to show lport ownership. */
 705		if (strlcat(buf, "NVME RPORT       ", PAGE_SIZE) >= PAGE_SIZE)
 706			goto unlock_buf_done;
 707		if (phba->brd_no >= 10) {
 708			if (strlcat(buf, " ", PAGE_SIZE) >= PAGE_SIZE)
 709				goto unlock_buf_done;
 710		}
 711
 712		scnprintf(tmp, sizeof(tmp), "WWPN x%llx ",
 713			  nrport->port_name);
 714		if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 715			goto unlock_buf_done;
 716
 717		scnprintf(tmp, sizeof(tmp), "WWNN x%llx ",
 718			  nrport->node_name);
 719		if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 720			goto unlock_buf_done;
 721
 722		scnprintf(tmp, sizeof(tmp), "DID x%06x ",
 723			  nrport->port_id);
 724		if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 725			goto unlock_buf_done;
 726
 727		/* An NVME rport can have multiple roles. */
 728		if (nrport->port_role & FC_PORT_ROLE_NVME_INITIATOR) {
 729			if (strlcat(buf, "INITIATOR ", PAGE_SIZE) >= PAGE_SIZE)
 730				goto unlock_buf_done;
 731		}
 732		if (nrport->port_role & FC_PORT_ROLE_NVME_TARGET) {
 733			if (strlcat(buf, "TARGET ", PAGE_SIZE) >= PAGE_SIZE)
 734				goto unlock_buf_done;
 735		}
 736		if (nrport->port_role & FC_PORT_ROLE_NVME_DISCOVERY) {
 737			if (strlcat(buf, "DISCSRVC ", PAGE_SIZE) >= PAGE_SIZE)
 738				goto unlock_buf_done;
 739		}
 740		if (nrport->port_role & ~(FC_PORT_ROLE_NVME_INITIATOR |
 741					  FC_PORT_ROLE_NVME_TARGET |
 742					  FC_PORT_ROLE_NVME_DISCOVERY)) {
 743			scnprintf(tmp, sizeof(tmp), "UNKNOWN ROLE x%x",
 744				  nrport->port_role);
 745			if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 746				goto unlock_buf_done;
 747		}
 748
 749		scnprintf(tmp, sizeof(tmp), "%s\n", statep);
 750		if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 751			goto unlock_buf_done;
 752	}
 753	spin_unlock_irqrestore(&vport->fc_nodes_list_lock, iflags);
 754
 755	if (!lport)
 756		goto buffer_done;
 757
 758	if (strlcat(buf, "\nNVME Statistics\n", PAGE_SIZE) >= PAGE_SIZE)
 759		goto buffer_done;
 760
 761	scnprintf(tmp, sizeof(tmp),
 762		  "LS: Xmt %010x Cmpl %010x Abort %08x\n",
 763		  atomic_read(&lport->fc4NvmeLsRequests),
 764		  atomic_read(&lport->fc4NvmeLsCmpls),
 765		  atomic_read(&lport->xmt_ls_abort));
 766	if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 767		goto buffer_done;
 768
 769	scnprintf(tmp, sizeof(tmp),
 770		  "LS XMIT: Err %08x  CMPL: xb %08x Err %08x\n",
 771		  atomic_read(&lport->xmt_ls_err),
 772		  atomic_read(&lport->cmpl_ls_xb),
 773		  atomic_read(&lport->cmpl_ls_err));
 774	if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 775		goto buffer_done;
 776
 777	totin = 0;
 778	totout = 0;
 779	for (i = 0; i < phba->cfg_hdw_queue; i++) {
 780		cstat = &phba->sli4_hba.hdwq[i].nvme_cstat;
 781		tot = cstat->io_cmpls;
 782		totin += tot;
 783		data1 = cstat->input_requests;
 784		data2 = cstat->output_requests;
 785		data3 = cstat->control_requests;
 786		totout += (data1 + data2 + data3);
 787	}
 788	scnprintf(tmp, sizeof(tmp),
 789		  "Total FCP Cmpl %016llx Issue %016llx "
 790		  "OutIO %016llx\n",
 791		  totin, totout, totout - totin);
 792	if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 793		goto buffer_done;
 794
 795	scnprintf(tmp, sizeof(tmp),
 796		  "\tabort %08x noxri %08x nondlp %08x qdepth %08x "
 797		  "wqerr %08x err %08x\n",
 798		  atomic_read(&lport->xmt_fcp_abort),
 799		  atomic_read(&lport->xmt_fcp_noxri),
 800		  atomic_read(&lport->xmt_fcp_bad_ndlp),
 801		  atomic_read(&lport->xmt_fcp_qdepth),
 802		  atomic_read(&lport->xmt_fcp_wqerr),
 803		  atomic_read(&lport->xmt_fcp_err));
 804	if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 805		goto buffer_done;
 806
 807	scnprintf(tmp, sizeof(tmp),
 808		  "FCP CMPL: xb %08x Err %08x\n",
 809		  atomic_read(&lport->cmpl_fcp_xb),
 810		  atomic_read(&lport->cmpl_fcp_err));
 811	strlcat(buf, tmp, PAGE_SIZE);
 812
 813	/* host_lock is already unlocked. */
 814	goto buffer_done;
 815
 816 unlock_buf_done:
 817	spin_unlock_irqrestore(&vport->fc_nodes_list_lock, iflags);
 818
 819 buffer_done:
 820	len = strnlen(buf, PAGE_SIZE);
 821
 822	if (unlikely(len >= (PAGE_SIZE - 1))) {
 823		lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
 824				"6314 Catching potential buffer "
 825				"overflow > PAGE_SIZE = %lu bytes\n",
 826				PAGE_SIZE);
 827		strscpy(buf + PAGE_SIZE - 1 - sizeof(LPFC_INFO_MORE_STR),
 828			LPFC_INFO_MORE_STR,
 829			sizeof(LPFC_INFO_MORE_STR) + 1);
 830	}
 831
 832	return len;
 833}
 834
 835static ssize_t
 836lpfc_scsi_stat_show(struct device *dev, struct device_attribute *attr,
 837		    char *buf)
 838{
 839	struct Scsi_Host *shost = class_to_shost(dev);
 840	struct lpfc_vport *vport = shost_priv(shost);
 841	struct lpfc_hba *phba = vport->phba;
 842	int len;
 843	struct lpfc_fc4_ctrl_stat *cstat;
 844	u64 data1, data2, data3;
 845	u64 tot, totin, totout;
 846	int i;
 847	char tmp[LPFC_MAX_SCSI_INFO_TMP_LEN] = {0};
 848
 849	if (!(vport->cfg_enable_fc4_type & LPFC_ENABLE_FCP) ||
 850	    (phba->sli_rev != LPFC_SLI_REV4))
 851		return 0;
 852
 853	scnprintf(buf, PAGE_SIZE, "SCSI HDWQ Statistics\n");
 854
 855	totin = 0;
 856	totout = 0;
 857	for (i = 0; i < phba->cfg_hdw_queue; i++) {
 858		cstat = &phba->sli4_hba.hdwq[i].scsi_cstat;
 859		tot = cstat->io_cmpls;
 860		totin += tot;
 861		data1 = cstat->input_requests;
 862		data2 = cstat->output_requests;
 863		data3 = cstat->control_requests;
 864		totout += (data1 + data2 + data3);
 865
 866		scnprintf(tmp, sizeof(tmp), "HDWQ (%d): Rd %016llx Wr %016llx "
 867			  "IO %016llx ", i, data1, data2, data3);
 868		if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 869			goto buffer_done;
 870
 871		scnprintf(tmp, sizeof(tmp), "Cmpl %016llx OutIO %016llx\n",
 872			  tot, ((data1 + data2 + data3) - tot));
 873		if (strlcat(buf, tmp, PAGE_SIZE) >= PAGE_SIZE)
 874			goto buffer_done;
 875	}
 876	scnprintf(tmp, sizeof(tmp), "Total FCP Cmpl %016llx Issue %016llx "
 877		  "OutIO %016llx\n", totin, totout, totout - totin);
 878	strlcat(buf, tmp, PAGE_SIZE);
 879
 880buffer_done:
 881	len = strnlen(buf, PAGE_SIZE);
 882
 883	return len;
 884}
 885
 886static ssize_t
 887lpfc_bg_info_show(struct device *dev, struct device_attribute *attr,
 888		  char *buf)
 889{
 890	struct Scsi_Host *shost = class_to_shost(dev);
 891	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
 892	struct lpfc_hba   *phba = vport->phba;
 893
 894	if (phba->cfg_enable_bg) {
 895		if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
 896			return scnprintf(buf, PAGE_SIZE,
 897					"BlockGuard Enabled\n");
 898		else
 899			return scnprintf(buf, PAGE_SIZE,
 900					"BlockGuard Not Supported\n");
 901	} else
 902		return scnprintf(buf, PAGE_SIZE,
 903					"BlockGuard Disabled\n");
 904}
 905
 906static ssize_t
 907lpfc_bg_guard_err_show(struct device *dev, struct device_attribute *attr,
 908		       char *buf)
 909{
 910	struct Scsi_Host *shost = class_to_shost(dev);
 911	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
 912	struct lpfc_hba   *phba = vport->phba;
 913
 914	return scnprintf(buf, PAGE_SIZE, "%llu\n",
 915			(unsigned long long)phba->bg_guard_err_cnt);
 916}
 917
 918static ssize_t
 919lpfc_bg_apptag_err_show(struct device *dev, struct device_attribute *attr,
 920			char *buf)
 921{
 922	struct Scsi_Host *shost = class_to_shost(dev);
 923	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
 924	struct lpfc_hba   *phba = vport->phba;
 925
 926	return scnprintf(buf, PAGE_SIZE, "%llu\n",
 927			(unsigned long long)phba->bg_apptag_err_cnt);
 928}
 929
 930static ssize_t
 931lpfc_bg_reftag_err_show(struct device *dev, struct device_attribute *attr,
 932			char *buf)
 933{
 934	struct Scsi_Host *shost = class_to_shost(dev);
 935	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
 936	struct lpfc_hba   *phba = vport->phba;
 937
 938	return scnprintf(buf, PAGE_SIZE, "%llu\n",
 939			(unsigned long long)phba->bg_reftag_err_cnt);
 940}
 941
 942/**
 943 * lpfc_info_show - Return some pci info about the host in ascii
 944 * @dev: class converted to a Scsi_host structure.
 945 * @attr: device attribute, not used.
 946 * @buf: on return contains the formatted text from lpfc_info().
 947 *
 948 * Returns: size of formatted string.
 949 **/
 950static ssize_t
 951lpfc_info_show(struct device *dev, struct device_attribute *attr,
 952	       char *buf)
 953{
 954	struct Scsi_Host *host = class_to_shost(dev);
 955
 956	return scnprintf(buf, PAGE_SIZE, "%s\n", lpfc_info(host));
 957}
 958
 959/**
 960 * lpfc_serialnum_show - Return the hba serial number in ascii
 961 * @dev: class converted to a Scsi_host structure.
 962 * @attr: device attribute, not used.
 963 * @buf: on return contains the formatted text serial number.
 964 *
 965 * Returns: size of formatted string.
 966 **/
 967static ssize_t
 968lpfc_serialnum_show(struct device *dev, struct device_attribute *attr,
 969		    char *buf)
 970{
 971	struct Scsi_Host  *shost = class_to_shost(dev);
 972	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
 973	struct lpfc_hba   *phba = vport->phba;
 974
 975	return scnprintf(buf, PAGE_SIZE, "%s\n", phba->SerialNumber);
 976}
 977
 978/**
 979 * lpfc_temp_sensor_show - Return the temperature sensor level
 980 * @dev: class converted to a Scsi_host structure.
 981 * @attr: device attribute, not used.
 982 * @buf: on return contains the formatted support level.
 983 *
 984 * Description:
 985 * Returns a number indicating the temperature sensor level currently
 986 * supported, zero or one in ascii.
 987 *
 988 * Returns: size of formatted string.
 989 **/
 990static ssize_t
 991lpfc_temp_sensor_show(struct device *dev, struct device_attribute *attr,
 992		      char *buf)
 993{
 994	struct Scsi_Host *shost = class_to_shost(dev);
 995	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
 996	struct lpfc_hba   *phba = vport->phba;
 997	return scnprintf(buf, PAGE_SIZE, "%d\n", phba->temp_sensor_support);
 998}
 999
1000/**
1001 * lpfc_modeldesc_show - Return the model description of the hba
1002 * @dev: class converted to a Scsi_host structure.
1003 * @attr: device attribute, not used.
1004 * @buf: on return contains the scsi vpd model description.
1005 *
1006 * Returns: size of formatted string.
1007 **/
1008static ssize_t
1009lpfc_modeldesc_show(struct device *dev, struct device_attribute *attr,
1010		    char *buf)
1011{
1012	struct Scsi_Host  *shost = class_to_shost(dev);
1013	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1014	struct lpfc_hba   *phba = vport->phba;
1015
1016	return scnprintf(buf, PAGE_SIZE, "%s\n", phba->ModelDesc);
1017}
1018
1019/**
1020 * lpfc_modelname_show - Return the model name of the hba
1021 * @dev: class converted to a Scsi_host structure.
1022 * @attr: device attribute, not used.
1023 * @buf: on return contains the scsi vpd model name.
1024 *
1025 * Returns: size of formatted string.
1026 **/
1027static ssize_t
1028lpfc_modelname_show(struct device *dev, struct device_attribute *attr,
1029		    char *buf)
1030{
1031	struct Scsi_Host  *shost = class_to_shost(dev);
1032	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1033	struct lpfc_hba   *phba = vport->phba;
1034
1035	return scnprintf(buf, PAGE_SIZE, "%s\n", phba->ModelName);
1036}
1037
1038/**
1039 * lpfc_programtype_show - Return the program type of the hba
1040 * @dev: class converted to a Scsi_host structure.
1041 * @attr: device attribute, not used.
1042 * @buf: on return contains the scsi vpd program type.
1043 *
1044 * Returns: size of formatted string.
1045 **/
1046static ssize_t
1047lpfc_programtype_show(struct device *dev, struct device_attribute *attr,
1048		      char *buf)
1049{
1050	struct Scsi_Host  *shost = class_to_shost(dev);
1051	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1052	struct lpfc_hba   *phba = vport->phba;
1053
1054	return scnprintf(buf, PAGE_SIZE, "%s\n", phba->ProgramType);
1055}
1056
1057/**
1058 * lpfc_vportnum_show - Return the port number in ascii of the hba
1059 * @dev: class converted to a Scsi_host structure.
1060 * @attr: device attribute, not used.
1061 * @buf: on return contains scsi vpd program type.
1062 *
1063 * Returns: size of formatted string.
1064 **/
1065static ssize_t
1066lpfc_vportnum_show(struct device *dev, struct device_attribute *attr,
1067		   char *buf)
1068{
1069	struct Scsi_Host  *shost = class_to_shost(dev);
1070	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1071	struct lpfc_hba   *phba = vport->phba;
1072
1073	return scnprintf(buf, PAGE_SIZE, "%s\n", phba->Port);
1074}
1075
1076/**
1077 * lpfc_fwrev_show - Return the firmware rev running in the hba
1078 * @dev: class converted to a Scsi_host structure.
1079 * @attr: device attribute, not used.
1080 * @buf: on return contains the scsi vpd program type.
1081 *
1082 * Returns: size of formatted string.
1083 **/
1084static ssize_t
1085lpfc_fwrev_show(struct device *dev, struct device_attribute *attr,
1086		char *buf)
1087{
1088	struct Scsi_Host  *shost = class_to_shost(dev);
1089	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1090	struct lpfc_hba   *phba = vport->phba;
1091	uint32_t if_type;
1092	uint8_t sli_family;
1093	char fwrev[FW_REV_STR_SIZE];
1094	int len;
1095
1096	lpfc_decode_firmware_rev(phba, fwrev, 1);
1097	if_type = phba->sli4_hba.pc_sli4_params.if_type;
1098	sli_family = phba->sli4_hba.pc_sli4_params.sli_family;
1099
1100	if (phba->sli_rev < LPFC_SLI_REV4)
1101		len = scnprintf(buf, PAGE_SIZE, "%s, sli-%d\n",
1102			       fwrev, phba->sli_rev);
1103	else
1104		len = scnprintf(buf, PAGE_SIZE, "%s, sli-%d:%d:%x\n",
1105			       fwrev, phba->sli_rev, if_type, sli_family);
1106
1107	return len;
1108}
1109
1110/**
1111 * lpfc_hdw_show - Return the jedec information about the hba
1112 * @dev: class converted to a Scsi_host structure.
1113 * @attr: device attribute, not used.
1114 * @buf: on return contains the scsi vpd program type.
1115 *
1116 * Returns: size of formatted string.
1117 **/
1118static ssize_t
1119lpfc_hdw_show(struct device *dev, struct device_attribute *attr, char *buf)
1120{
1121	char hdw[9];
1122	struct Scsi_Host  *shost = class_to_shost(dev);
1123	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1124	struct lpfc_hba   *phba = vport->phba;
1125	lpfc_vpd_t *vp = &phba->vpd;
1126
1127	lpfc_jedec_to_ascii(vp->rev.biuRev, hdw);
1128	return scnprintf(buf, PAGE_SIZE, "%s %08x %08x\n", hdw,
1129			 vp->rev.smRev, vp->rev.smFwRev);
1130}
1131
1132/**
1133 * lpfc_option_rom_version_show - Return the adapter ROM FCode version
1134 * @dev: class converted to a Scsi_host structure.
1135 * @attr: device attribute, not used.
1136 * @buf: on return contains the ROM and FCode ascii strings.
1137 *
1138 * Returns: size of formatted string.
1139 **/
1140static ssize_t
1141lpfc_option_rom_version_show(struct device *dev, struct device_attribute *attr,
1142			     char *buf)
1143{
1144	struct Scsi_Host  *shost = class_to_shost(dev);
1145	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1146	struct lpfc_hba   *phba = vport->phba;
1147	char fwrev[FW_REV_STR_SIZE];
1148
1149	if (phba->sli_rev < LPFC_SLI_REV4)
1150		return scnprintf(buf, PAGE_SIZE, "%s\n",
1151				phba->OptionROMVersion);
1152
1153	lpfc_decode_firmware_rev(phba, fwrev, 1);
1154	return scnprintf(buf, PAGE_SIZE, "%s\n", fwrev);
1155}
1156
1157/**
1158 * lpfc_link_state_show - Return the link state of the port
1159 * @dev: class converted to a Scsi_host structure.
1160 * @attr: device attribute, not used.
1161 * @buf: on return contains text describing the state of the link.
1162 *
1163 * Notes:
1164 * The switch statement has no default so zero will be returned.
1165 *
1166 * Returns: size of formatted string.
1167 **/
1168static ssize_t
1169lpfc_link_state_show(struct device *dev, struct device_attribute *attr,
1170		     char *buf)
1171{
1172	struct Scsi_Host  *shost = class_to_shost(dev);
1173	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1174	struct lpfc_hba   *phba = vport->phba;
1175	int  len = 0;
1176
1177	switch (phba->link_state) {
1178	case LPFC_LINK_UNKNOWN:
1179	case LPFC_WARM_START:
1180	case LPFC_INIT_START:
1181	case LPFC_INIT_MBX_CMDS:
1182	case LPFC_LINK_DOWN:
1183	case LPFC_HBA_ERROR:
1184		if (test_bit(LINK_DISABLED, &phba->hba_flag))
1185			len += scnprintf(buf + len, PAGE_SIZE-len,
1186				"Link Down - User disabled\n");
1187		else
1188			len += scnprintf(buf + len, PAGE_SIZE-len,
1189				"Link Down\n");
1190		break;
1191	case LPFC_LINK_UP:
1192	case LPFC_CLEAR_LA:
1193	case LPFC_HBA_READY:
1194		len += scnprintf(buf + len, PAGE_SIZE-len, "Link Up - ");
1195
1196		switch (vport->port_state) {
1197		case LPFC_LOCAL_CFG_LINK:
1198			len += scnprintf(buf + len, PAGE_SIZE-len,
1199					"Configuring Link\n");
1200			break;
1201		case LPFC_FDISC:
1202		case LPFC_FLOGI:
1203		case LPFC_FABRIC_CFG_LINK:
1204		case LPFC_NS_REG:
1205		case LPFC_NS_QRY:
1206		case LPFC_BUILD_DISC_LIST:
1207		case LPFC_DISC_AUTH:
1208			len += scnprintf(buf + len, PAGE_SIZE - len,
1209					"Discovery\n");
1210			break;
1211		case LPFC_VPORT_READY:
1212			len += scnprintf(buf + len, PAGE_SIZE - len,
1213					"Ready\n");
1214			break;
1215
1216		case LPFC_VPORT_FAILED:
1217			len += scnprintf(buf + len, PAGE_SIZE - len,
1218					"Failed\n");
1219			break;
1220
1221		case LPFC_VPORT_UNKNOWN:
1222			len += scnprintf(buf + len, PAGE_SIZE - len,
1223					"Unknown\n");
1224			break;
1225		}
1226		if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
1227			if (test_bit(FC_PUBLIC_LOOP, &vport->fc_flag))
1228				len += scnprintf(buf + len, PAGE_SIZE-len,
1229						"   Public Loop\n");
1230			else
1231				len += scnprintf(buf + len, PAGE_SIZE-len,
1232						"   Private Loop\n");
1233		} else {
1234			if (test_bit(FC_FABRIC, &vport->fc_flag)) {
1235				if (phba->sli_rev == LPFC_SLI_REV4 &&
1236				    vport->port_type == LPFC_PHYSICAL_PORT &&
1237				    phba->sli4_hba.fawwpn_flag &
1238					LPFC_FAWWPN_FABRIC)
1239					len += scnprintf(buf + len,
1240							 PAGE_SIZE - len,
1241							 "   Fabric FA-PWWN\n");
1242				else
1243					len += scnprintf(buf + len,
1244							 PAGE_SIZE - len,
1245							 "   Fabric\n");
1246			} else {
1247				len += scnprintf(buf + len, PAGE_SIZE-len,
1248						"   Point-2-Point\n");
1249			}
1250		}
1251	}
1252
1253	if ((phba->sli_rev == LPFC_SLI_REV4) &&
1254	    ((bf_get(lpfc_sli_intf_if_type,
1255	     &phba->sli4_hba.sli_intf) ==
1256	     LPFC_SLI_INTF_IF_TYPE_6))) {
1257		struct lpfc_trunk_link link = phba->trunk_link;
1258
1259		if (bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba))
1260			len += scnprintf(buf + len, PAGE_SIZE - len,
1261				"Trunk port 0: Link %s %s\n",
1262				(link.link0.state == LPFC_LINK_UP) ?
1263				 "Up" : "Down. ",
1264				trunk_errmsg[link.link0.fault]);
1265
1266		if (bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba))
1267			len += scnprintf(buf + len, PAGE_SIZE - len,
1268				"Trunk port 1: Link %s %s\n",
1269				(link.link1.state == LPFC_LINK_UP) ?
1270				 "Up" : "Down. ",
1271				trunk_errmsg[link.link1.fault]);
1272
1273		if (bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba))
1274			len += scnprintf(buf + len, PAGE_SIZE - len,
1275				"Trunk port 2: Link %s %s\n",
1276				(link.link2.state == LPFC_LINK_UP) ?
1277				 "Up" : "Down. ",
1278				trunk_errmsg[link.link2.fault]);
1279
1280		if (bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba))
1281			len += scnprintf(buf + len, PAGE_SIZE - len,
1282				"Trunk port 3: Link %s %s\n",
1283				(link.link3.state == LPFC_LINK_UP) ?
1284				 "Up" : "Down. ",
1285				trunk_errmsg[link.link3.fault]);
1286
1287	}
1288
1289	return len;
1290}
1291
1292/**
1293 * lpfc_sli4_protocol_show - Return the fip mode of the HBA
1294 * @dev: class unused variable.
1295 * @attr: device attribute, not used.
1296 * @buf: on return contains the module description text.
1297 *
1298 * Returns: size of formatted string.
1299 **/
1300static ssize_t
1301lpfc_sli4_protocol_show(struct device *dev, struct device_attribute *attr,
1302			char *buf)
1303{
1304	struct Scsi_Host *shost = class_to_shost(dev);
1305	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1306	struct lpfc_hba *phba = vport->phba;
1307
1308	if (phba->sli_rev < LPFC_SLI_REV4)
1309		return scnprintf(buf, PAGE_SIZE, "fc\n");
1310
1311	if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL) {
1312		if (phba->sli4_hba.lnk_info.lnk_tp == LPFC_LNK_TYPE_GE)
1313			return scnprintf(buf, PAGE_SIZE, "fcoe\n");
1314		if (phba->sli4_hba.lnk_info.lnk_tp == LPFC_LNK_TYPE_FC)
1315			return scnprintf(buf, PAGE_SIZE, "fc\n");
1316	}
1317	return scnprintf(buf, PAGE_SIZE, "unknown\n");
1318}
1319
1320/**
1321 * lpfc_oas_supported_show - Return whether or not Optimized Access Storage
1322 *			    (OAS) is supported.
1323 * @dev: class unused variable.
1324 * @attr: device attribute, not used.
1325 * @buf: on return contains the module description text.
1326 *
1327 * Returns: size of formatted string.
1328 **/
1329static ssize_t
1330lpfc_oas_supported_show(struct device *dev, struct device_attribute *attr,
1331			char *buf)
1332{
1333	struct Scsi_Host *shost = class_to_shost(dev);
1334	struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
1335	struct lpfc_hba *phba = vport->phba;
1336
1337	return scnprintf(buf, PAGE_SIZE, "%d\n",
1338			phba->sli4_hba.pc_sli4_params.oas_supported);
1339}
1340
1341/**
1342 * lpfc_link_state_store - Transition the link_state on an HBA port
1343 * @dev: class device that is converted into a Scsi_host.
1344 * @attr: device attribute, not used.
1345 * @buf: one or more lpfc_polling_flags values.
1346 * @count: not used.
1347 *
1348 * Returns:
1349 * -EINVAL if the buffer is not "up" or "down"
1350 * return from link state change function if non-zero
1351 * length of the buf on success
1352 **/
1353static ssize_t
1354lpfc_link_state_store(struct device *dev, struct device_attribute *attr,
1355		const char *buf, size_t count)
1356{
1357	struct Scsi_Host  *shost = class_to_shost(dev);
1358	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1359	struct lpfc_hba   *phba = vport->phba;
1360
1361	int status = -EINVAL;
1362
1363	if ((strncmp(buf, "up", sizeof("up") - 1) == 0) &&
1364			(phba->link_state == LPFC_LINK_DOWN))
1365		status = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
1366	else if ((strncmp(buf, "down", sizeof("down") - 1) == 0) &&
1367			(phba->link_state >= LPFC_LINK_UP))
1368		status = phba->lpfc_hba_down_link(phba, MBX_NOWAIT);
1369
1370	if (status == 0)
1371		return strlen(buf);
1372	else
1373		return status;
1374}
1375
1376/**
1377 * lpfc_num_discovered_ports_show - Return sum of mapped and unmapped vports
1378 * @dev: class device that is converted into a Scsi_host.
1379 * @attr: device attribute, not used.
1380 * @buf: on return contains the sum of fc mapped and unmapped.
1381 *
1382 * Description:
1383 * Returns the ascii text number of the sum of the fc mapped and unmapped
1384 * vport counts.
1385 *
1386 * Returns: size of formatted string.
1387 **/
1388static ssize_t
1389lpfc_num_discovered_ports_show(struct device *dev,
1390			       struct device_attribute *attr, char *buf)
1391{
1392	struct Scsi_Host  *shost = class_to_shost(dev);
1393	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1394
1395	return scnprintf(buf, PAGE_SIZE, "%d\n",
1396			 atomic_read(&vport->fc_map_cnt) +
1397			 atomic_read(&vport->fc_unmap_cnt));
1398}
1399
1400/**
1401 * lpfc_issue_lip - Misnomer, name carried over from long ago
1402 * @shost: Scsi_Host pointer.
1403 *
1404 * Description:
1405 * Bring the link down gracefully then re-init the link. The firmware will
1406 * re-init the fiber channel interface as required. Does not issue a LIP.
1407 *
1408 * Returns:
1409 * -EPERM port offline or management commands are being blocked
1410 * -ENOMEM cannot allocate memory for the mailbox command
1411 * -EIO error sending the mailbox command
1412 * zero for success
1413 **/
1414static int
1415lpfc_issue_lip(struct Scsi_Host *shost)
1416{
1417	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1418	struct lpfc_hba   *phba = vport->phba;
1419	LPFC_MBOXQ_t *pmboxq;
1420	int mbxstatus = MBXERR_ERROR;
1421
1422	/*
1423	 * If the link is offline, disabled or BLOCK_MGMT_IO
1424	 * it doesn't make any sense to allow issue_lip
1425	 */
1426	if (test_bit(FC_OFFLINE_MODE, &vport->fc_flag) ||
1427	    test_bit(LINK_DISABLED, &phba->hba_flag) ||
1428	    (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO))
1429		return -EPERM;
1430
1431	pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
1432
1433	if (!pmboxq)
1434		return -ENOMEM;
1435
1436	memset((void *)pmboxq, 0, sizeof (LPFC_MBOXQ_t));
1437	pmboxq->u.mb.mbxCommand = MBX_DOWN_LINK;
1438	pmboxq->u.mb.mbxOwner = OWN_HOST;
1439
1440	if (test_bit(FC_PT2PT, &vport->fc_flag))
1441		clear_bit(FC_PT2PT_NO_NVME, &vport->fc_flag);
1442
1443	mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO * 2);
1444
1445	if ((mbxstatus == MBX_SUCCESS) &&
1446	    (pmboxq->u.mb.mbxStatus == 0 ||
1447	     pmboxq->u.mb.mbxStatus == MBXERR_LINK_DOWN)) {
1448		memset((void *)pmboxq, 0, sizeof (LPFC_MBOXQ_t));
1449		lpfc_init_link(phba, pmboxq, phba->cfg_topology,
1450			       phba->cfg_link_speed);
1451		mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
1452						     phba->fc_ratov * 2);
1453		if ((mbxstatus == MBX_SUCCESS) &&
1454		    (pmboxq->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
1455			lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
1456					"2859 SLI authentication is required "
1457					"for INIT_LINK but has not done yet\n");
1458	}
1459
1460	lpfc_set_loopback_flag(phba);
1461	if (mbxstatus != MBX_TIMEOUT)
1462		mempool_free(pmboxq, phba->mbox_mem_pool);
1463
1464	if (mbxstatus == MBXERR_ERROR)
1465		return -EIO;
1466
1467	return 0;
1468}
1469
1470int
1471lpfc_emptyq_wait(struct lpfc_hba *phba, struct list_head *q, spinlock_t *lock)
1472{
1473	int cnt = 0;
1474
1475	spin_lock_irq(lock);
1476	while (!list_empty(q)) {
1477		spin_unlock_irq(lock);
1478		msleep(20);
1479		if (cnt++ > 250) {  /* 5 secs */
1480			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1481					"0466 Outstanding IO when "
1482					"bringing Adapter offline\n");
1483				return 0;
1484		}
1485		spin_lock_irq(lock);
1486	}
1487	spin_unlock_irq(lock);
1488	return 1;
1489}
1490
1491/**
1492 * lpfc_do_offline - Issues a mailbox command to bring the link down
1493 * @phba: lpfc_hba pointer.
1494 * @type: LPFC_EVT_OFFLINE, LPFC_EVT_WARM_START, LPFC_EVT_KILL.
1495 *
1496 * Notes:
1497 * Assumes any error from lpfc_do_offline() will be negative.
1498 * Can wait up to 5 seconds for the port ring buffers count
1499 * to reach zero, prints a warning if it is not zero and continues.
1500 * lpfc_workq_post_event() returns a non-zero return code if call fails.
1501 *
1502 * Returns:
1503 * -EIO error posting the event
1504 * zero for success
1505 **/
1506static int
1507lpfc_do_offline(struct lpfc_hba *phba, uint32_t type)
1508{
1509	struct completion online_compl;
1510	struct lpfc_queue *qp = NULL;
1511	struct lpfc_sli_ring *pring;
1512	struct lpfc_sli *psli;
1513	int status = 0;
1514	int i;
1515	int rc;
1516
1517	init_completion(&online_compl);
1518	rc = lpfc_workq_post_event(phba, &status, &online_compl,
1519			      LPFC_EVT_OFFLINE_PREP);
1520	if (rc == 0)
1521		return -ENOMEM;
1522
1523	wait_for_completion(&online_compl);
1524
1525	if (status != 0)
1526		return -EIO;
1527
1528	psli = &phba->sli;
1529
1530	/*
1531	 * If freeing the queues have already started, don't access them.
1532	 * Otherwise set FREE_WAIT to indicate that queues are being used
1533	 * to hold the freeing process until we finish.
1534	 */
1535	spin_lock_irq(&phba->hbalock);
1536	if (!(psli->sli_flag & LPFC_QUEUE_FREE_INIT)) {
1537		psli->sli_flag |= LPFC_QUEUE_FREE_WAIT;
1538	} else {
1539		spin_unlock_irq(&phba->hbalock);
1540		goto skip_wait;
1541	}
1542	spin_unlock_irq(&phba->hbalock);
1543
1544	/* Wait a little for things to settle down, but not
1545	 * long enough for dev loss timeout to expire.
1546	 */
1547	if (phba->sli_rev != LPFC_SLI_REV4) {
1548		for (i = 0; i < psli->num_rings; i++) {
1549			pring = &psli->sli3_ring[i];
1550			if (!lpfc_emptyq_wait(phba, &pring->txcmplq,
1551					      &phba->hbalock))
1552				goto out;
1553		}
1554	} else {
1555		list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
1556			pring = qp->pring;
1557			if (!pring)
1558				continue;
1559			if (!lpfc_emptyq_wait(phba, &pring->txcmplq,
1560					      &pring->ring_lock))
1561				goto out;
1562		}
1563	}
1564out:
1565	spin_lock_irq(&phba->hbalock);
1566	psli->sli_flag &= ~LPFC_QUEUE_FREE_WAIT;
1567	spin_unlock_irq(&phba->hbalock);
1568
1569skip_wait:
1570	init_completion(&online_compl);
1571	rc = lpfc_workq_post_event(phba, &status, &online_compl, type);
1572	if (rc == 0)
1573		return -ENOMEM;
1574
1575	wait_for_completion(&online_compl);
1576
1577	if (status != 0)
1578		return -EIO;
1579
1580	return 0;
1581}
1582
1583/**
1584 * lpfc_reset_pci_bus - resets PCI bridge controller's secondary bus of an HBA
1585 * @phba: lpfc_hba pointer.
1586 *
1587 * Description:
1588 * Issues a PCI secondary bus reset for the phba->pcidev.
1589 *
1590 * Notes:
1591 * First walks the bus_list to ensure only PCI devices with Emulex
1592 * vendor id, device ids that support hot reset, only one occurrence
1593 * of function 0, and all ports on the bus are in offline mode to ensure the
1594 * hot reset only affects one valid HBA.
1595 *
1596 * Returns:
1597 * -ENOTSUPP, cfg_enable_hba_reset must be of value 2
1598 * -ENODEV,   NULL ptr to pcidev
1599 * -EBADSLT,  detected invalid device
1600 * -EBUSY,    port is not in offline state
1601 *      0,    successful
1602 */
1603static int
1604lpfc_reset_pci_bus(struct lpfc_hba *phba)
1605{
1606	struct pci_dev *pdev = phba->pcidev;
1607	struct Scsi_Host *shost = NULL;
1608	struct lpfc_hba *phba_other = NULL;
1609	struct pci_dev *ptr = NULL;
1610	int res;
1611
1612	if (phba->cfg_enable_hba_reset != 2)
1613		return -ENOTSUPP;
1614
1615	if (!pdev) {
1616		lpfc_printf_log(phba, KERN_INFO, LOG_INIT, "8345 pdev NULL!\n");
1617		return -ENODEV;
1618	}
1619
1620	res = lpfc_check_pci_resettable(phba);
1621	if (res)
1622		return res;
1623
1624	/* Walk the list of devices on the pci_dev's bus */
1625	list_for_each_entry(ptr, &pdev->bus->devices, bus_list) {
1626		/* Check port is offline */
1627		shost = pci_get_drvdata(ptr);
1628		if (shost) {
1629			phba_other =
1630				((struct lpfc_vport *)shost->hostdata)->phba;
1631			if (!test_bit(FC_OFFLINE_MODE,
1632				      &phba_other->pport->fc_flag)) {
1633				lpfc_printf_log(phba_other, KERN_INFO, LOG_INIT,
1634						"8349 WWPN = 0x%02x%02x%02x%02x"
1635						"%02x%02x%02x%02x is not "
1636						"offline!\n",
1637						phba_other->wwpn[0],
1638						phba_other->wwpn[1],
1639						phba_other->wwpn[2],
1640						phba_other->wwpn[3],
1641						phba_other->wwpn[4],
1642						phba_other->wwpn[5],
1643						phba_other->wwpn[6],
1644						phba_other->wwpn[7]);
1645				return -EBUSY;
1646			}
1647		}
1648	}
1649
1650	/* Issue PCI bus reset */
1651	res = pci_reset_bus(pdev);
1652	if (res) {
1653		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1654				"8350 PCI reset bus failed: %d\n", res);
1655	}
1656
1657	return res;
1658}
1659
1660/**
1661 * lpfc_selective_reset - Offline then onlines the port
1662 * @phba: lpfc_hba pointer.
1663 *
1664 * Description:
1665 * If the port is configured to allow a reset then the hba is brought
1666 * offline then online.
1667 *
1668 * Notes:
1669 * Assumes any error from lpfc_do_offline() will be negative.
1670 * Do not make this function static.
1671 *
1672 * Returns:
1673 * lpfc_do_offline() return code if not zero
1674 * -EIO reset not configured or error posting the event
1675 * zero for success
1676 **/
1677int
1678lpfc_selective_reset(struct lpfc_hba *phba)
1679{
1680	struct completion online_compl;
1681	int status = 0;
1682	int rc;
1683
1684	if (!phba->cfg_enable_hba_reset)
1685		return -EACCES;
1686
1687	if (!test_bit(FC_OFFLINE_MODE, &phba->pport->fc_flag)) {
1688		status = lpfc_do_offline(phba, LPFC_EVT_OFFLINE);
1689
1690		if (status != 0)
1691			return status;
1692	}
1693
1694	init_completion(&online_compl);
1695	rc = lpfc_workq_post_event(phba, &status, &online_compl,
1696			      LPFC_EVT_ONLINE);
1697	if (rc == 0)
1698		return -ENOMEM;
1699
1700	wait_for_completion(&online_compl);
1701
1702	if (status != 0)
1703		return -EIO;
1704
1705	return 0;
1706}
1707
1708/**
1709 * lpfc_issue_reset - Selectively resets an adapter
1710 * @dev: class device that is converted into a Scsi_host.
1711 * @attr: device attribute, not used.
1712 * @buf: containing the string "selective".
1713 * @count: unused variable.
1714 *
1715 * Description:
1716 * If the buf contains the string "selective" then lpfc_selective_reset()
1717 * is called to perform the reset.
1718 *
1719 * Notes:
1720 * Assumes any error from lpfc_selective_reset() will be negative.
1721 * If lpfc_selective_reset() returns zero then the length of the buffer
1722 * is returned which indicates success
1723 *
1724 * Returns:
1725 * -EINVAL if the buffer does not contain the string "selective"
1726 * length of buf if lpfc-selective_reset() if the call succeeds
1727 * return value of lpfc_selective_reset() if the call fails
1728**/
1729static ssize_t
1730lpfc_issue_reset(struct device *dev, struct device_attribute *attr,
1731		 const char *buf, size_t count)
1732{
1733	struct Scsi_Host  *shost = class_to_shost(dev);
1734	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1735	struct lpfc_hba   *phba = vport->phba;
1736	int status = -EINVAL;
1737
1738	if (!phba->cfg_enable_hba_reset)
1739		return -EACCES;
1740
1741	if (strncmp(buf, "selective", sizeof("selective") - 1) == 0)
1742		status = phba->lpfc_selective_reset(phba);
1743
1744	if (status == 0)
1745		return strlen(buf);
1746	else
1747		return status;
1748}
1749
1750/**
1751 * lpfc_sli4_pdev_status_reg_wait - Wait for pdev status register for readyness
1752 * @phba: lpfc_hba pointer.
1753 *
1754 * Description:
1755 * SLI4 interface type-2 device to wait on the sliport status register for
1756 * the readyness after performing a firmware reset.
1757 *
1758 * Returns:
1759 * zero for success, -EPERM when port does not have privilege to perform the
1760 * reset, -EIO when port timeout from recovering from the reset.
1761 *
1762 * Note:
1763 * As the caller will interpret the return code by value, be careful in making
1764 * change or addition to return codes.
1765 **/
1766int
1767lpfc_sli4_pdev_status_reg_wait(struct lpfc_hba *phba)
1768{
1769	struct lpfc_register portstat_reg = {0};
1770	int i;
1771
1772	msleep(100);
1773	if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
1774		       &portstat_reg.word0))
1775		return -EIO;
1776
1777	/* verify if privileged for the request operation */
1778	if (!bf_get(lpfc_sliport_status_rn, &portstat_reg) &&
1779	    !bf_get(lpfc_sliport_status_err, &portstat_reg))
1780		return -EPERM;
1781
1782	/* There is no point to wait if the port is in an unrecoverable
1783	 * state.
1784	 */
1785	if (lpfc_sli4_unrecoverable_port(&portstat_reg))
1786		return -EIO;
1787
1788	/* wait for the SLI port firmware ready after firmware reset */
1789	for (i = 0; i < LPFC_FW_RESET_MAXIMUM_WAIT_10MS_CNT; i++) {
1790		msleep(10);
1791		if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
1792			       &portstat_reg.word0))
1793			continue;
1794		if (!bf_get(lpfc_sliport_status_err, &portstat_reg))
1795			continue;
1796		if (!bf_get(lpfc_sliport_status_rn, &portstat_reg))
1797			continue;
1798		if (!bf_get(lpfc_sliport_status_rdy, &portstat_reg))
1799			continue;
1800		break;
1801	}
1802
1803	if (i < LPFC_FW_RESET_MAXIMUM_WAIT_10MS_CNT)
1804		return 0;
1805	else
1806		return -EIO;
1807}
1808
1809/**
1810 * lpfc_sli4_pdev_reg_request - Request physical dev to perform a register acc
1811 * @phba: lpfc_hba pointer.
1812 * @opcode: The sli4 config command opcode.
1813 *
1814 * Description:
1815 * Request SLI4 interface type-2 device to perform a physical register set
1816 * access.
1817 *
1818 * Returns:
1819 * zero for success
1820 **/
1821static ssize_t
1822lpfc_sli4_pdev_reg_request(struct lpfc_hba *phba, uint32_t opcode)
1823{
1824	struct completion online_compl;
1825	struct pci_dev *pdev = phba->pcidev;
1826	unsigned long before_fc_flag;
1827	uint32_t sriov_nr_virtfn;
1828	uint32_t reg_val;
1829	int status = 0, rc = 0;
1830	int job_posted = 1, sriov_err;
1831
1832	if (!phba->cfg_enable_hba_reset)
1833		return -EACCES;
1834
1835	if ((phba->sli_rev < LPFC_SLI_REV4) ||
1836	    (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
1837	     LPFC_SLI_INTF_IF_TYPE_2))
1838		return -EPERM;
1839
1840	/* Keep state if we need to restore back */
1841	before_fc_flag = phba->pport->fc_flag;
1842	sriov_nr_virtfn = phba->cfg_sriov_nr_virtfn;
1843
1844	if (opcode == LPFC_FW_DUMP) {
1845		init_completion(&online_compl);
1846		phba->fw_dump_cmpl = &online_compl;
1847	} else {
1848		/* Disable SR-IOV virtual functions if enabled */
1849		if (phba->cfg_sriov_nr_virtfn) {
1850			pci_disable_sriov(pdev);
1851			phba->cfg_sriov_nr_virtfn = 0;
1852		}
1853
1854		status = lpfc_do_offline(phba, LPFC_EVT_OFFLINE);
1855
1856		if (status != 0)
1857			return status;
1858
1859		/* wait for the device to be quiesced before firmware reset */
1860		msleep(100);
1861	}
1862
1863	reg_val = readl(phba->sli4_hba.conf_regs_memmap_p +
1864			LPFC_CTL_PDEV_CTL_OFFSET);
1865
1866	if (opcode == LPFC_FW_DUMP)
1867		reg_val |= LPFC_FW_DUMP_REQUEST;
1868	else if (opcode == LPFC_FW_RESET)
1869		reg_val |= LPFC_CTL_PDEV_CTL_FRST;
1870	else if (opcode == LPFC_DV_RESET)
1871		reg_val |= LPFC_CTL_PDEV_CTL_DRST;
1872
1873	writel(reg_val, phba->sli4_hba.conf_regs_memmap_p +
1874	       LPFC_CTL_PDEV_CTL_OFFSET);
1875	/* flush */
1876	readl(phba->sli4_hba.conf_regs_memmap_p + LPFC_CTL_PDEV_CTL_OFFSET);
1877
1878	/* delay driver action following IF_TYPE_2 reset */
1879	rc = lpfc_sli4_pdev_status_reg_wait(phba);
1880
1881	if (rc == -EPERM) {
1882		/* no privilege for reset */
1883		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1884				"3150 No privilege to perform the requested "
1885				"access: x%x\n", reg_val);
1886	} else if (rc == -EIO) {
1887		/* reset failed, there is nothing more we can do */
1888		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1889				"3153 Fail to perform the requested "
1890				"access: x%x\n", reg_val);
1891		if (phba->fw_dump_cmpl)
1892			phba->fw_dump_cmpl = NULL;
1893		return rc;
1894	}
1895
1896	/* keep the original port state */
1897	if (test_bit(FC_OFFLINE_MODE, &before_fc_flag)) {
1898		if (phba->fw_dump_cmpl)
1899			phba->fw_dump_cmpl = NULL;
1900		goto out;
1901	}
1902
1903	/* Firmware dump will trigger an HA_ERATT event, and
1904	 * lpfc_handle_eratt_s4 routine already handles bringing the port back
1905	 * online.
1906	 */
1907	if (opcode == LPFC_FW_DUMP) {
1908		wait_for_completion(phba->fw_dump_cmpl);
1909	} else  {
1910		init_completion(&online_compl);
1911		job_posted = lpfc_workq_post_event(phba, &status, &online_compl,
1912						   LPFC_EVT_ONLINE);
1913		if (!job_posted)
1914			goto out;
1915
1916		wait_for_completion(&online_compl);
1917	}
1918out:
1919	/* in any case, restore the virtual functions enabled as before */
1920	if (sriov_nr_virtfn) {
1921		/* If fw_dump was performed, first disable to clean up */
1922		if (opcode == LPFC_FW_DUMP) {
1923			pci_disable_sriov(pdev);
1924			phba->cfg_sriov_nr_virtfn = 0;
1925		}
1926
1927		sriov_err =
1928			lpfc_sli_probe_sriov_nr_virtfn(phba, sriov_nr_virtfn);
1929		if (!sriov_err)
1930			phba->cfg_sriov_nr_virtfn = sriov_nr_virtfn;
1931	}
1932
1933	/* return proper error code */
1934	if (!rc) {
1935		if (!job_posted)
1936			rc = -ENOMEM;
1937		else if (status)
1938			rc = -EIO;
1939	}
1940	return rc;
1941}
1942
1943/**
1944 * lpfc_nport_evt_cnt_show - Return the number of nport events
1945 * @dev: class device that is converted into a Scsi_host.
1946 * @attr: device attribute, not used.
1947 * @buf: on return contains the ascii number of nport events.
1948 *
1949 * Returns: size of formatted string.
1950 **/
1951static ssize_t
1952lpfc_nport_evt_cnt_show(struct device *dev, struct device_attribute *attr,
1953			char *buf)
1954{
1955	struct Scsi_Host  *shost = class_to_shost(dev);
1956	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1957	struct lpfc_hba   *phba = vport->phba;
1958
1959	return scnprintf(buf, PAGE_SIZE, "%d\n", phba->nport_event_cnt);
1960}
1961
1962static int
1963lpfc_set_trunking(struct lpfc_hba *phba, char *buff_out)
1964{
1965	LPFC_MBOXQ_t *mbox = NULL;
1966	u32 payload_len;
1967	unsigned long val = 0;
1968	char *pval = NULL;
1969	int rc = 0;
1970
1971	if (!strncmp("enable", buff_out,
1972				 strlen("enable"))) {
1973		pval = buff_out + strlen("enable") + 1;
1974		rc = kstrtoul(pval, 0, &val);
1975		if (rc)
1976			return rc; /* Invalid  number */
1977	} else if (!strncmp("disable", buff_out,
1978				 strlen("disable"))) {
1979		val = 0;
1980	} else {
1981		return -EINVAL;  /* Invalid command */
1982	}
1983
1984	switch (val) {
1985	case 0:
1986		val = 0x0; /* Disable */
1987		break;
1988	case 2:
1989		val = 0x1; /* Enable two port trunk */
1990		break;
1991	case 4:
1992		val = 0x2; /* Enable four port trunk */
1993		break;
1994	default:
1995		return -EINVAL;
1996	}
1997
1998	lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1999			"0070 Set trunk mode with val %ld ", val);
2000
2001	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2002	if (!mbox)
2003		return -ENOMEM;
2004
2005	payload_len = sizeof(struct lpfc_mbx_set_trunk_mode) -
2006		      sizeof(struct lpfc_sli4_cfg_mhdr);
2007	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
2008			 LPFC_MBOX_OPCODE_FCOE_FC_SET_TRUNK_MODE,
2009			 payload_len, LPFC_SLI4_MBX_EMBED);
2010
2011	bf_set(lpfc_mbx_set_trunk_mode,
2012	       &mbox->u.mqe.un.set_trunk_mode,
2013	       val);
2014	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
2015	if (rc)
2016		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
2017				"0071 Set trunk mode failed with status: %d",
2018				rc);
2019	mempool_free(mbox, phba->mbox_mem_pool);
2020
2021	return 0;
2022}
2023
2024static ssize_t
2025lpfc_xcvr_data_show(struct device *dev, struct device_attribute *attr,
2026		    char *buf)
2027{
2028	struct Scsi_Host  *shost = class_to_shost(dev);
2029	struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
2030	struct lpfc_hba   *phba = vport->phba;
2031	int rc;
2032	int len = 0;
2033	struct lpfc_rdp_context	*rdp_context;
2034	u16 temperature;
2035	u16 rx_power;
2036	u16 tx_bias;
2037	u16 tx_power;
2038	u16 vcc;
2039	char chbuf[128];
2040	u16 wavelength = 0;
2041	struct sff_trasnceiver_codes_byte7 *trasn_code_byte7;
2042
2043	/* Get transceiver information */
2044	rdp_context = kmalloc(sizeof(*rdp_context), GFP_KERNEL);
2045	if (!rdp_context) {
2046		len = scnprintf(buf, PAGE_SIZE - len,
2047				"SPF info NA: alloc failure\n");
2048		return len;
2049	}
2050
2051	rc = lpfc_get_sfp_info_wait(phba, rdp_context);
2052	if (rc) {
2053		len = scnprintf(buf, PAGE_SIZE - len, "SFP info NA:\n");
2054		goto out_free_rdp;
2055	}
2056
2057	strscpy(chbuf, &rdp_context->page_a0[SSF_VENDOR_NAME], 16);
2058
2059	len = scnprintf(buf, PAGE_SIZE - len, "VendorName:\t%s\n", chbuf);
2060	len += scnprintf(buf + len, PAGE_SIZE - len,
2061			 "VendorOUI:\t%02x-%02x-%02x\n",
2062			 (uint8_t)rdp_context->page_a0[SSF_VENDOR_OUI],
2063			 (uint8_t)rdp_context->page_a0[SSF_VENDOR_OUI + 1],
2064			 (uint8_t)rdp_context->page_a0[SSF_VENDOR_OUI + 2]);
2065	strscpy(chbuf, &rdp_context->page_a0[SSF_VENDOR_PN], 16);
2066	len += scnprintf(buf + len, PAGE_SIZE - len, "VendorPN:\t%s\n", chbuf);
2067	strscpy(chbuf, &rdp_context->page_a0[SSF_VENDOR_SN], 16);
2068	len += scnprintf(buf + len, PAGE_SIZE - len, "VendorSN:\t%s\n", chbuf);
2069	strscpy(chbuf, &rdp_context->page_a0[SSF_VENDOR_REV], 4);
2070	len += scnprintf(buf + len, PAGE_SIZE - len, "VendorRev:\t%s\n", chbuf);
2071	strscpy(chbuf, &rdp_context->page_a0[SSF_DATE_CODE], 8);
2072	len += scnprintf(buf + len, PAGE_SIZE - len, "DateCode:\t%s\n", chbuf);
2073	len += scnprintf(buf + len, PAGE_SIZE - len, "Identifier:\t%xh\n",
2074			 (uint8_t)rdp_context->page_a0[SSF_IDENTIFIER]);
2075	len += scnprintf(buf + len, PAGE_SIZE - len, "ExtIdentifier:\t%xh\n",
2076			 (uint8_t)rdp_context->page_a0[SSF_EXT_IDENTIFIER]);
2077	len += scnprintf(buf + len, PAGE_SIZE - len, "Connector:\t%xh\n",
2078			 (uint8_t)rdp_context->page_a0[SSF_CONNECTOR]);
2079	wavelength = (rdp_context->page_a0[SSF_WAVELENGTH_B1] << 8) |
2080		      rdp_context->page_a0[SSF_WAVELENGTH_B0];
2081
2082	len += scnprintf(buf + len, PAGE_SIZE - len, "Wavelength:\t%d nm\n",
2083			 wavelength);
2084	trasn_code_byte7 = (struct sff_trasnceiver_codes_byte7 *)
2085			&rdp_context->page_a0[SSF_TRANSCEIVER_CODE_B7];
2086
2087	len += scnprintf(buf + len, PAGE_SIZE - len, "Speeds: \t");
2088	if (*(uint8_t *)trasn_code_byte7 == 0) {
2089		len += scnprintf(buf + len, PAGE_SIZE - len, "Unknown\n");
2090	} else {
2091		if (trasn_code_byte7->fc_sp_100MB)
2092			len += scnprintf(buf + len, PAGE_SIZE - len, "1 ");
2093		if (trasn_code_byte7->fc_sp_200mb)
2094			len += scnprintf(buf + len, PAGE_SIZE - len, "2 ");
2095		if (trasn_code_byte7->fc_sp_400MB)
2096			len += scnprintf(buf + len, PAGE_SIZE - len, "4 ");
2097		if (trasn_code_byte7->fc_sp_800MB)
2098			len += scnprintf(buf + len, PAGE_SIZE - len, "8 ");
2099		if (trasn_code_byte7->fc_sp_1600MB)
2100			len += scnprintf(buf + len, PAGE_SIZE - len, "16 ");
2101		if (trasn_code_byte7->fc_sp_3200MB)
2102			len += scnprintf(buf + len, PAGE_SIZE - len, "32 ");
2103		if (trasn_code_byte7->speed_chk_ecc)
2104			len += scnprintf(buf + len, PAGE_SIZE - len, "64 ");
2105		len += scnprintf(buf + len, PAGE_SIZE - len, "GB\n");
2106	}
2107	temperature = (rdp_context->page_a2[SFF_TEMPERATURE_B1] << 8 |
2108		       rdp_context->page_a2[SFF_TEMPERATURE_B0]);
2109	vcc = (rdp_context->page_a2[SFF_VCC_B1] << 8 |
2110	       rdp_context->page_a2[SFF_VCC_B0]);
2111	tx_power = (rdp_context->page_a2[SFF_TXPOWER_B1] << 8 |
2112		    rdp_context->page_a2[SFF_TXPOWER_B0]);
2113	tx_bias = (rdp_context->page_a2[SFF_TX_BIAS_CURRENT_B1] << 8 |
2114		   rdp_context->page_a2[SFF_TX_BIAS_CURRENT_B0]);
2115	rx_power = (rdp_context->page_a2[SFF_RXPOWER_B1] << 8 |
2116		    rdp_context->page_a2[SFF_RXPOWER_B0]);
2117
2118	len += scnprintf(buf + len, PAGE_SIZE - len,
2119			 "Temperature:\tx%04x C\n", temperature);
2120	len += scnprintf(buf + len, PAGE_SIZE - len, "Vcc:\t\tx%04x V\n", vcc);
2121	len += scnprintf(buf + len, PAGE_SIZE - len,
2122			 "TxBiasCurrent:\tx%04x mA\n", tx_bias);
2123	len += scnprintf(buf + len, PAGE_SIZE - len, "TxPower:\tx%04x mW\n",
2124			 tx_power);
2125	len += scnprintf(buf + len, PAGE_SIZE - len, "RxPower:\tx%04x mW\n",
2126			 rx_power);
2127out_free_rdp:
2128	kfree(rdp_context);
2129	return len;
2130}
2131
2132/**
2133 * lpfc_board_mode_show - Return the state of the board
2134 * @dev: class device that is converted into a Scsi_host.
2135 * @attr: device attribute, not used.
2136 * @buf: on return contains the state of the adapter.
2137 *
2138 * Returns: size of formatted string.
2139 **/
2140static ssize_t
2141lpfc_board_mode_show(struct device *dev, struct device_attribute *attr,
2142		     char *buf)
2143{
2144	struct Scsi_Host  *shost = class_to_shost(dev);
2145	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2146	struct lpfc_hba   *phba = vport->phba;
2147	char  * state;
2148
2149	if (phba->link_state == LPFC_HBA_ERROR)
2150		state = "error";
2151	else if (phba->link_state == LPFC_WARM_START)
2152		state = "warm start";
2153	else if (phba->link_state == LPFC_INIT_START)
2154		state = "offline";
2155	else
2156		state = "online";
2157
2158	return scnprintf(buf, PAGE_SIZE, "%s\n", state);
2159}
2160
2161/**
2162 * lpfc_board_mode_store - Puts the hba in online, offline, warm or error state
2163 * @dev: class device that is converted into a Scsi_host.
2164 * @attr: device attribute, not used.
2165 * @buf: containing one of the strings "online", "offline", "warm" or "error".
2166 * @count: unused variable.
2167 *
2168 * Returns:
2169 * -EACCES if enable hba reset not enabled
2170 * -EINVAL if the buffer does not contain a valid string (see above)
2171 * -EIO if lpfc_workq_post_event() or lpfc_do_offline() fails
2172 * buf length greater than zero indicates success
2173 **/
2174static ssize_t
2175lpfc_board_mode_store(struct device *dev, struct device_attribute *attr,
2176		      const char *buf, size_t count)
2177{
2178	struct Scsi_Host  *shost = class_to_shost(dev);
2179	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2180	struct lpfc_hba   *phba = vport->phba;
2181	struct completion online_compl;
2182	char *board_mode_str = NULL;
2183	int status = 0;
2184	int rc;
2185
2186	if (!phba->cfg_enable_hba_reset) {
2187		status = -EACCES;
2188		goto board_mode_out;
2189	}
2190
2191	lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
2192			 "3050 lpfc_board_mode set to %s\n", buf);
2193
2194	init_completion(&online_compl);
2195
2196	if(strncmp(buf, "online", sizeof("online") - 1) == 0) {
2197		rc = lpfc_workq_post_event(phba, &status, &online_compl,
2198				      LPFC_EVT_ONLINE);
2199		if (rc == 0) {
2200			status = -ENOMEM;
2201			goto board_mode_out;
2202		}
2203		wait_for_completion(&online_compl);
2204		if (status)
2205			status = -EIO;
2206	} else if (strncmp(buf, "offline", sizeof("offline") - 1) == 0)
2207		status = lpfc_do_offline(phba, LPFC_EVT_OFFLINE);
2208	else if (strncmp(buf, "warm", sizeof("warm") - 1) == 0)
2209		if (phba->sli_rev == LPFC_SLI_REV4)
2210			status = -EINVAL;
2211		else
2212			status = lpfc_do_offline(phba, LPFC_EVT_WARM_START);
2213	else if (strncmp(buf, "error", sizeof("error") - 1) == 0)
2214		if (phba->sli_rev == LPFC_SLI_REV4)
2215			status = -EINVAL;
2216		else
2217			status = lpfc_do_offline(phba, LPFC_EVT_KILL);
2218	else if (strncmp(buf, "dump", sizeof("dump") - 1) == 0)
2219		status = lpfc_sli4_pdev_reg_request(phba, LPFC_FW_DUMP);
2220	else if (strncmp(buf, "fw_reset", sizeof("fw_reset") - 1) == 0)
2221		status = lpfc_sli4_pdev_reg_request(phba, LPFC_FW_RESET);
2222	else if (strncmp(buf, "dv_reset", sizeof("dv_reset") - 1) == 0)
2223		status = lpfc_sli4_pdev_reg_request(phba, LPFC_DV_RESET);
2224	else if (strncmp(buf, "pci_bus_reset", sizeof("pci_bus_reset") - 1)
2225		 == 0)
2226		status = lpfc_reset_pci_bus(phba);
2227	else if (strncmp(buf, "heartbeat", sizeof("heartbeat") - 1) == 0)
2228		lpfc_issue_hb_tmo(phba);
2229	else if (strncmp(buf, "trunk", sizeof("trunk") - 1) == 0)
2230		status = lpfc_set_trunking(phba, (char *)buf + sizeof("trunk"));
2231	else
2232		status = -EINVAL;
2233
2234board_mode_out:
2235	if (!status)
2236		return strlen(buf);
2237	else {
2238		board_mode_str = strchr(buf, '\n');
2239		if (board_mode_str)
2240			*board_mode_str = '\0';
2241		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
2242				 "3097 Failed \"%s\", status(%d), "
2243				 "fc_flag(x%lx)\n",
2244				 buf, status, phba->pport->fc_flag);
2245		return status;
2246	}
2247}
2248
2249/**
2250 * lpfc_get_hba_info - Return various bits of informaton about the adapter
2251 * @phba: pointer to the adapter structure.
2252 * @mxri: max xri count.
2253 * @axri: available xri count.
2254 * @mrpi: max rpi count.
2255 * @arpi: available rpi count.
2256 * @mvpi: max vpi count.
2257 * @avpi: available vpi count.
2258 *
2259 * Description:
2260 * If an integer pointer for an count is not null then the value for the
2261 * count is returned.
2262 *
2263 * Returns:
2264 * zero on error
2265 * one for success
2266 **/
2267static int
2268lpfc_get_hba_info(struct lpfc_hba *phba,
2269		  uint32_t *mxri, uint32_t *axri,
2270		  uint32_t *mrpi, uint32_t *arpi,
2271		  uint32_t *mvpi, uint32_t *avpi)
2272{
2273	LPFC_MBOXQ_t *pmboxq;
2274	MAILBOX_t *pmb;
2275	int rc = 0;
2276	struct lpfc_sli4_hba *sli4_hba;
2277	struct lpfc_max_cfg_param *max_cfg_param;
2278	u16 rsrc_ext_cnt, rsrc_ext_size, max_vpi;
2279
2280	/*
2281	 * prevent udev from issuing mailbox commands until the port is
2282	 * configured.
2283	 */
2284	if (phba->link_state < LPFC_LINK_DOWN ||
2285	    !phba->mbox_mem_pool ||
2286	    (phba->sli.sli_flag & LPFC_SLI_ACTIVE) == 0)
2287		return 0;
2288
2289	if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO)
2290		return 0;
2291
2292	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2293	if (!pmboxq)
2294		return 0;
2295	memset(pmboxq, 0, sizeof (LPFC_MBOXQ_t));
2296
2297	pmb = &pmboxq->u.mb;
2298	pmb->mbxCommand = MBX_READ_CONFIG;
2299	pmb->mbxOwner = OWN_HOST;
2300	pmboxq->ctx_buf = NULL;
2301
2302	if (test_bit(FC_OFFLINE_MODE, &phba->pport->fc_flag))
2303		rc = MBX_NOT_FINISHED;
2304	else
2305		rc = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
2306
2307	if (rc != MBX_SUCCESS) {
2308		if (rc != MBX_TIMEOUT)
2309			mempool_free(pmboxq, phba->mbox_mem_pool);
2310		return 0;
2311	}
2312
2313	if (phba->sli_rev == LPFC_SLI_REV4) {
2314		sli4_hba = &phba->sli4_hba;
2315		max_cfg_param = &sli4_hba->max_cfg_param;
2316
2317		/* Normally, extents are not used */
2318		if (!phba->sli4_hba.extents_in_use) {
2319			if (mrpi)
2320				*mrpi = max_cfg_param->max_rpi;
2321			if (mxri)
2322				*mxri = max_cfg_param->max_xri;
2323			if (mvpi) {
2324				max_vpi = max_cfg_param->max_vpi;
2325
2326				/* Limit the max we support */
2327				if (max_vpi > LPFC_MAX_VPI)
2328					max_vpi = LPFC_MAX_VPI;
2329				*mvpi = max_vpi;
2330			}
2331		} else { /* Extents in use */
2332			if (mrpi) {
2333				if (lpfc_sli4_get_avail_extnt_rsrc(phba,
2334								   LPFC_RSC_TYPE_FCOE_RPI,
2335								   &rsrc_ext_cnt,
2336								   &rsrc_ext_size)) {
2337					rc = 0;
2338					goto free_pmboxq;
2339				}
2340
2341				*mrpi = rsrc_ext_cnt * rsrc_ext_size;
2342			}
2343
2344			if (mxri) {
2345				if (lpfc_sli4_get_avail_extnt_rsrc(phba,
2346								   LPFC_RSC_TYPE_FCOE_XRI,
2347								   &rsrc_ext_cnt,
2348								   &rsrc_ext_size)) {
2349					rc = 0;
2350					goto free_pmboxq;
2351				}
2352
2353				*mxri = rsrc_ext_cnt * rsrc_ext_size;
2354			}
2355
2356			if (mvpi) {
2357				if (lpfc_sli4_get_avail_extnt_rsrc(phba,
2358								   LPFC_RSC_TYPE_FCOE_VPI,
2359								   &rsrc_ext_cnt,
2360								   &rsrc_ext_size)) {
2361					rc = 0;
2362					goto free_pmboxq;
2363				}
2364
2365				max_vpi = rsrc_ext_cnt * rsrc_ext_size;
2366
2367				/* Limit the max we support */
2368				if (max_vpi > LPFC_MAX_VPI)
2369					max_vpi = LPFC_MAX_VPI;
2370				*mvpi = max_vpi;
2371			}
2372		}
2373	} else {
2374		if (mrpi)
2375			*mrpi = pmb->un.varRdConfig.max_rpi;
2376		if (arpi)
2377			*arpi = pmb->un.varRdConfig.avail_rpi;
2378		if (mxri)
2379			*mxri = pmb->un.varRdConfig.max_xri;
2380		if (axri)
2381			*axri = pmb->un.varRdConfig.avail_xri;
2382		if (mvpi)
2383			*mvpi = pmb->un.varRdConfig.max_vpi;
2384		if (avpi) {
2385			/* avail_vpi is only valid if link is up and ready */
2386			if (phba->link_state == LPFC_HBA_READY)
2387				*avpi = pmb->un.varRdConfig.avail_vpi;
2388			else
2389				*avpi = pmb->un.varRdConfig.max_vpi;
2390		}
2391	}
2392
2393	/* Success */
2394	rc = 1;
2395
2396free_pmboxq:
2397	mempool_free(pmboxq, phba->mbox_mem_pool);
2398	return rc;
2399}
2400
2401/**
2402 * lpfc_max_rpi_show - Return maximum rpi
2403 * @dev: class device that is converted into a Scsi_host.
2404 * @attr: device attribute, not used.
2405 * @buf: on return contains the maximum rpi count in decimal or "Unknown".
2406 *
2407 * Description:
2408 * Calls lpfc_get_hba_info() asking for just the mrpi count.
2409 * If lpfc_get_hba_info() returns zero (failure) the buffer text is set
2410 * to "Unknown" and the buffer length is returned, therefore the caller
2411 * must check for "Unknown" in the buffer to detect a failure.
2412 *
2413 * Returns: size of formatted string.
2414 **/
2415static ssize_t
2416lpfc_max_rpi_show(struct device *dev, struct device_attribute *attr,
2417		  char *buf)
2418{
2419	struct Scsi_Host  *shost = class_to_shost(dev);
2420	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2421	struct lpfc_hba   *phba = vport->phba;
2422	uint32_t cnt;
2423
2424	if (lpfc_get_hba_info(phba, NULL, NULL, &cnt, NULL, NULL, NULL))
2425		return scnprintf(buf, PAGE_SIZE, "%d\n", cnt);
2426	return scnprintf(buf, PAGE_SIZE, "Unknown\n");
2427}
2428
2429/**
2430 * lpfc_used_rpi_show - Return maximum rpi minus available rpi
2431 * @dev: class device that is converted into a Scsi_host.
2432 * @attr: device attribute, not used.
2433 * @buf: containing the used rpi count in decimal or "Unknown".
2434 *
2435 * Description:
2436 * Calls lpfc_get_hba_info() asking for just the mrpi and arpi counts.
2437 * If lpfc_get_hba_info() returns zero (failure) the buffer text is set
2438 * to "Unknown" and the buffer length is returned, therefore the caller
2439 * must check for "Unknown" in the buffer to detect a failure.
2440 *
2441 * Returns: size of formatted string.
2442 **/
2443static ssize_t
2444lpfc_used_rpi_show(struct device *dev, struct device_attribute *attr,
2445		   char *buf)
2446{
2447	struct Scsi_Host  *shost = class_to_shost(dev);
2448	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2449	struct lpfc_hba   *phba = vport->phba;
2450	struct lpfc_sli4_hba *sli4_hba;
2451	struct lpfc_max_cfg_param *max_cfg_param;
2452	u32 cnt = 0, acnt = 0;
2453
2454	if (phba->sli_rev == LPFC_SLI_REV4) {
2455		sli4_hba = &phba->sli4_hba;
2456		max_cfg_param = &sli4_hba->max_cfg_param;
2457		return scnprintf(buf, PAGE_SIZE, "%d\n",
2458				 max_cfg_param->rpi_used);
2459	} else {
2460		if (lpfc_get_hba_info(phba, NULL, NULL, &cnt, &acnt, NULL, NULL))
2461			return scnprintf(buf, PAGE_SIZE, "%d\n", (cnt - acnt));
2462	}
2463	return scnprintf(buf, PAGE_SIZE, "Unknown\n");
2464}
2465
2466/**
2467 * lpfc_max_xri_show - Return maximum xri
2468 * @dev: class device that is converted into a Scsi_host.
2469 * @attr: device attribute, not used.
2470 * @buf: on return contains the maximum xri count in decimal or "Unknown".
2471 *
2472 * Description:
2473 * Calls lpfc_get_hba_info() asking for just the mrpi count.
2474 * If lpfc_get_hba_info() returns zero (failure) the buffer text is set
2475 * to "Unknown" and the buffer length is returned, therefore the caller
2476 * must check for "Unknown" in the buffer to detect a failure.
2477 *
2478 * Returns: size of formatted string.
2479 **/
2480static ssize_t
2481lpfc_max_xri_show(struct device *dev, struct device_attribute *attr,
2482		  char *buf)
2483{
2484	struct Scsi_Host  *shost = class_to_shost(dev);
2485	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2486	struct lpfc_hba   *phba = vport->phba;
2487	uint32_t cnt;
2488
2489	if (lpfc_get_hba_info(phba, &cnt, NULL, NULL, NULL, NULL, NULL))
2490		return scnprintf(buf, PAGE_SIZE, "%d\n", cnt);
2491	return scnprintf(buf, PAGE_SIZE, "Unknown\n");
2492}
2493
2494/**
2495 * lpfc_used_xri_show - Return maximum xpi minus the available xpi
2496 * @dev: class device that is converted into a Scsi_host.
2497 * @attr: device attribute, not used.
2498 * @buf: on return contains the used xri count in decimal or "Unknown".
2499 *
2500 * Description:
2501 * Calls lpfc_get_hba_info() asking for just the mxri and axri counts.
2502 * If lpfc_get_hba_info() returns zero (failure) the buffer text is set
2503 * to "Unknown" and the buffer length is returned, therefore the caller
2504 * must check for "Unknown" in the buffer to detect a failure.
2505 *
2506 * Returns: size of formatted string.
2507 **/
2508static ssize_t
2509lpfc_used_xri_show(struct device *dev, struct device_attribute *attr,
2510		   char *buf)
2511{
2512	struct Scsi_Host  *shost = class_to_shost(dev);
2513	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2514	struct lpfc_hba   *phba = vport->phba;
2515	struct lpfc_sli4_hba *sli4_hba;
2516	struct lpfc_max_cfg_param *max_cfg_param;
2517	u32 cnt = 0, acnt = 0;
2518
2519	if (phba->sli_rev == LPFC_SLI_REV4) {
2520		sli4_hba = &phba->sli4_hba;
2521		max_cfg_param = &sli4_hba->max_cfg_param;
2522		return scnprintf(buf, PAGE_SIZE, "%d\n",
2523				 max_cfg_param->xri_used);
2524	} else {
2525		if (lpfc_get_hba_info(phba, &cnt, &acnt, NULL, NULL, NULL, NULL))
2526			return scnprintf(buf, PAGE_SIZE, "%d\n", (cnt - acnt));
2527	}
2528	return scnprintf(buf, PAGE_SIZE, "Unknown\n");
2529}
2530
2531/**
2532 * lpfc_max_vpi_show - Return maximum vpi
2533 * @dev: class device that is converted into a Scsi_host.
2534 * @attr: device attribute, not used.
2535 * @buf: on return contains the maximum vpi count in decimal or "Unknown".
2536 *
2537 * Description:
2538 * Calls lpfc_get_hba_info() asking for just the mvpi count.
2539 * If lpfc_get_hba_info() returns zero (failure) the buffer text is set
2540 * to "Unknown" and the buffer length is returned, therefore the caller
2541 * must check for "Unknown" in the buffer to detect a failure.
2542 *
2543 * Returns: size of formatted string.
2544 **/
2545static ssize_t
2546lpfc_max_vpi_show(struct device *dev, struct device_attribute *attr,
2547		  char *buf)
2548{
2549	struct Scsi_Host  *shost = class_to_shost(dev);
2550	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2551	struct lpfc_hba   *phba = vport->phba;
2552	uint32_t cnt;
2553
2554	if (lpfc_get_hba_info(phba, NULL, NULL, NULL, NULL, &cnt, NULL))
2555		return scnprintf(buf, PAGE_SIZE, "%d\n", cnt);
2556	return scnprintf(buf, PAGE_SIZE, "Unknown\n");
2557}
2558
2559/**
2560 * lpfc_used_vpi_show - Return maximum vpi minus the available vpi
2561 * @dev: class device that is converted into a Scsi_host.
2562 * @attr: device attribute, not used.
2563 * @buf: on return contains the used vpi count in decimal or "Unknown".
2564 *
2565 * Description:
2566 * Calls lpfc_get_hba_info() asking for just the mvpi and avpi counts.
2567 * If lpfc_get_hba_info() returns zero (failure) the buffer text is set
2568 * to "Unknown" and the buffer length is returned, therefore the caller
2569 * must check for "Unknown" in the buffer to detect a failure.
2570 *
2571 * Returns: size of formatted string.
2572 **/
2573static ssize_t
2574lpfc_used_vpi_show(struct device *dev, struct device_attribute *attr,
2575		   char *buf)
2576{
2577	struct Scsi_Host  *shost = class_to_shost(dev);
2578	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2579	struct lpfc_hba   *phba = vport->phba;
2580	struct lpfc_sli4_hba *sli4_hba;
2581	struct lpfc_max_cfg_param *max_cfg_param;
2582	u32 cnt = 0, acnt = 0;
2583
2584	if (phba->sli_rev == LPFC_SLI_REV4) {
2585		sli4_hba = &phba->sli4_hba;
2586		max_cfg_param = &sli4_hba->max_cfg_param;
2587		return scnprintf(buf, PAGE_SIZE, "%d\n",
2588				 max_cfg_param->vpi_used);
2589	} else {
2590		if (lpfc_get_hba_info(phba, NULL, NULL, NULL, NULL, &cnt, &acnt))
2591			return scnprintf(buf, PAGE_SIZE, "%d\n", (cnt - acnt));
2592	}
2593	return scnprintf(buf, PAGE_SIZE, "Unknown\n");
2594}
2595
2596/**
2597 * lpfc_npiv_info_show - Return text about NPIV support for the adapter
2598 * @dev: class device that is converted into a Scsi_host.
2599 * @attr: device attribute, not used.
2600 * @buf: text that must be interpreted to determine if npiv is supported.
2601 *
2602 * Description:
2603 * Buffer will contain text indicating npiv is not suppoerted on the port,
2604 * the port is an NPIV physical port, or it is an npiv virtual port with
2605 * the id of the vport.
2606 *
2607 * Returns: size of formatted string.
2608 **/
2609static ssize_t
2610lpfc_npiv_info_show(struct device *dev, struct device_attribute *attr,
2611		    char *buf)
2612{
2613	struct Scsi_Host  *shost = class_to_shost(dev);
2614	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2615	struct lpfc_hba   *phba = vport->phba;
2616
2617	if (!(phba->max_vpi))
2618		return scnprintf(buf, PAGE_SIZE, "NPIV Not Supported\n");
2619	if (vport->port_type == LPFC_PHYSICAL_PORT)
2620		return scnprintf(buf, PAGE_SIZE, "NPIV Physical\n");
2621	return scnprintf(buf, PAGE_SIZE, "NPIV Virtual (VPI %d)\n", vport->vpi);
2622}
2623
2624/**
2625 * lpfc_poll_show - Return text about poll support for the adapter
2626 * @dev: class device that is converted into a Scsi_host.
2627 * @attr: device attribute, not used.
2628 * @buf: on return contains the cfg_poll in hex.
2629 *
2630 * Notes:
2631 * cfg_poll should be a lpfc_polling_flags type.
2632 *
2633 * Returns: size of formatted string.
2634 **/
2635static ssize_t
2636lpfc_poll_show(struct device *dev, struct device_attribute *attr,
2637	       char *buf)
2638{
2639	struct Scsi_Host  *shost = class_to_shost(dev);
2640	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2641	struct lpfc_hba   *phba = vport->phba;
2642
2643	return scnprintf(buf, PAGE_SIZE, "%#x\n", phba->cfg_poll);
2644}
2645
2646/**
2647 * lpfc_poll_store - Set the value of cfg_poll for the adapter
2648 * @dev: class device that is converted into a Scsi_host.
2649 * @attr: device attribute, not used.
2650 * @buf: one or more lpfc_polling_flags values.
2651 * @count: not used.
2652 *
2653 * Notes:
2654 * buf contents converted to integer and checked for a valid value.
2655 *
2656 * Returns:
2657 * -EINVAL if the buffer connot be converted or is out of range
2658 * length of the buf on success
2659 **/
2660static ssize_t
2661lpfc_poll_store(struct device *dev, struct device_attribute *attr,
2662		const char *buf, size_t count)
2663{
2664	struct Scsi_Host  *shost = class_to_shost(dev);
2665	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2666	struct lpfc_hba   *phba = vport->phba;
2667	uint32_t creg_val;
2668	uint32_t old_val;
2669	int val=0;
2670
2671	if (!isdigit(buf[0]))
2672		return -EINVAL;
2673
2674	if (sscanf(buf, "%i", &val) != 1)
2675		return -EINVAL;
2676
2677	if ((val & 0x3) != val)
2678		return -EINVAL;
2679
2680	if (phba->sli_rev == LPFC_SLI_REV4)
2681		val = 0;
2682
2683	lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
2684		"3051 lpfc_poll changed from %d to %d\n",
2685		phba->cfg_poll, val);
2686
2687	spin_lock_irq(&phba->hbalock);
2688
2689	old_val = phba->cfg_poll;
2690
2691	if (val & ENABLE_FCP_RING_POLLING) {
2692		if ((val & DISABLE_FCP_RING_INT) &&
2693		    !(old_val & DISABLE_FCP_RING_INT)) {
2694			if (lpfc_readl(phba->HCregaddr, &creg_val)) {
2695				spin_unlock_irq(&phba->hbalock);
2696				return -EINVAL;
2697			}
2698			creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
2699			writel(creg_val, phba->HCregaddr);
2700			readl(phba->HCregaddr); /* flush */
2701
2702			lpfc_poll_start_timer(phba);
2703		}
2704	} else if (val != 0x0) {
2705		spin_unlock_irq(&phba->hbalock);
2706		return -EINVAL;
2707	}
2708
2709	if (!(val & DISABLE_FCP_RING_INT) &&
2710	    (old_val & DISABLE_FCP_RING_INT))
2711	{
2712		spin_unlock_irq(&phba->hbalock);
2713		timer_delete(&phba->fcp_poll_timer);
2714		spin_lock_irq(&phba->hbalock);
2715		if (lpfc_readl(phba->HCregaddr, &creg_val)) {
2716			spin_unlock_irq(&phba->hbalock);
2717			return -EINVAL;
2718		}
2719		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
2720		writel(creg_val, phba->HCregaddr);
2721		readl(phba->HCregaddr); /* flush */
2722	}
2723
2724	phba->cfg_poll = val;
2725
2726	spin_unlock_irq(&phba->hbalock);
2727
2728	return strlen(buf);
2729}
2730
2731/**
2732 * lpfc_sriov_hw_max_virtfn_show - Return maximum number of virtual functions
2733 * @dev: class converted to a Scsi_host structure.
2734 * @attr: device attribute, not used.
2735 * @buf: on return contains the formatted support level.
2736 *
2737 * Description:
2738 * Returns the maximum number of virtual functions a physical function can
2739 * support, 0 will be returned if called on virtual function.
2740 *
2741 * Returns: size of formatted string.
2742 **/
2743static ssize_t
2744lpfc_sriov_hw_max_virtfn_show(struct device *dev,
2745			      struct device_attribute *attr,
2746			      char *buf)
2747{
2748	struct Scsi_Host *shost = class_to_shost(dev);
2749	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2750	struct lpfc_hba *phba = vport->phba;
2751	uint16_t max_nr_virtfn;
2752
2753	max_nr_virtfn = lpfc_sli_sriov_nr_virtfn_get(phba);
2754	return scnprintf(buf, PAGE_SIZE, "%d\n", max_nr_virtfn);
2755}
2756
2757/**
2758 * lpfc_enable_bbcr_set: Sets an attribute value.
2759 * @phba: pointer to the adapter structure.
2760 * @val: integer attribute value.
2761 *
2762 * Description:
2763 * Validates the min and max values then sets the
2764 * adapter config field if in the valid range. prints error message
2765 * and does not set the parameter if invalid.
2766 *
2767 * Returns:
2768 * zero on success
2769 * -EINVAL if val is invalid
2770 */
2771static ssize_t
2772lpfc_enable_bbcr_set(struct lpfc_hba *phba, uint val)
2773{
2774	if (lpfc_rangecheck(val, 0, 1) && phba->sli_rev == LPFC_SLI_REV4) {
2775		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2776				"3068 lpfc_enable_bbcr changed from %d to "
2777				"%d\n", phba->cfg_enable_bbcr, val);
2778		phba->cfg_enable_bbcr = val;
2779		return 0;
2780	}
2781	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2782			"0451 lpfc_enable_bbcr cannot set to %d, range is 0, "
2783			"1\n", val);
2784	return -EINVAL;
2785}
2786
2787/*
2788 * lpfc_param_show - Return a cfg attribute value in decimal
2789 *
2790 * Description:
2791 * Macro that given an attr e.g. hba_queue_depth expands
2792 * into a function with the name lpfc_hba_queue_depth_show.
2793 *
2794 * lpfc_##attr##_show: Return the decimal value of an adapters cfg_xxx field.
2795 * @dev: class device that is converted into a Scsi_host.
2796 * @attr: device attribute, not used.
2797 * @buf: on return contains the attribute value in decimal.
2798 *
2799 * Returns: size of formatted string.
2800 **/
2801#define lpfc_param_show(attr)	\
2802static ssize_t \
2803lpfc_##attr##_show(struct device *dev, struct device_attribute *attr, \
2804		   char *buf) \
2805{ \
2806	struct Scsi_Host  *shost = class_to_shost(dev);\
2807	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;\
2808	struct lpfc_hba   *phba = vport->phba;\
2809	return scnprintf(buf, PAGE_SIZE, "%d\n",\
2810			phba->cfg_##attr);\
2811}
2812
2813/*
2814 * lpfc_param_hex_show - Return a cfg attribute value in hex
2815 *
2816 * Description:
2817 * Macro that given an attr e.g. hba_queue_depth expands
2818 * into a function with the name lpfc_hba_queue_depth_show
2819 *
2820 * lpfc_##attr##_show: Return the hex value of an adapters cfg_xxx field.
2821 * @dev: class device that is converted into a Scsi_host.
2822 * @attr: device attribute, not used.
2823 * @buf: on return contains the attribute value in hexadecimal.
2824 *
2825 * Returns: size of formatted string.
2826 **/
2827#define lpfc_param_hex_show(attr)	\
2828static ssize_t \
2829lpfc_##attr##_show(struct device *dev, struct device_attribute *attr, \
2830		   char *buf) \
2831{ \
2832	struct Scsi_Host  *shost = class_to_shost(dev);\
2833	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;\
2834	struct lpfc_hba   *phba = vport->phba;\
2835	uint val = 0;\
2836	val = phba->cfg_##attr;\
2837	return scnprintf(buf, PAGE_SIZE, "%#x\n",\
2838			phba->cfg_##attr);\
2839}
2840
2841/*
2842 * lpfc_param_init - Initializes a cfg attribute
2843 *
2844 * Description:
2845 * Macro that given an attr e.g. hba_queue_depth expands
2846 * into a function with the name lpfc_hba_queue_depth_init. The macro also
2847 * takes a default argument, a minimum and maximum argument.
2848 *
2849 * lpfc_##attr##_init: Initializes an attribute.
2850 * @phba: pointer to the adapter structure.
2851 * @val: integer attribute value.
2852 *
2853 * Validates the min and max values then sets the adapter config field
2854 * accordingly, or uses the default if out of range and prints an error message.
2855 *
2856 * Returns:
2857 * zero on success
2858 * -EINVAL if default used
2859 **/
2860#define lpfc_param_init(attr, default, minval, maxval)	\
2861static int \
2862lpfc_##attr##_init(struct lpfc_hba *phba, uint val) \
2863{ \
2864	if (lpfc_rangecheck(val, minval, maxval)) {\
2865		phba->cfg_##attr = val;\
2866		return 0;\
2867	}\
2868	lpfc_printf_log(phba, KERN_ERR, LOG_INIT, \
2869			"0449 lpfc_"#attr" attribute cannot be set to %d, "\
2870			"allowed range is ["#minval", "#maxval"]\n", val); \
2871	phba->cfg_##attr = default;\
2872	return -EINVAL;\
2873}
2874
2875/*
2876 * lpfc_param_set - Set a cfg attribute value
2877 *
2878 * Description:
2879 * Macro that given an attr e.g. hba_queue_depth expands
2880 * into a function with the name lpfc_hba_queue_depth_set
2881 *
2882 * lpfc_##attr##_set: Sets an attribute value.
2883 * @phba: pointer to the adapter structure.
2884 * @val: integer attribute value.
2885 *
2886 * Description:
2887 * Validates the min and max values then sets the
2888 * adapter config field if in the valid range. prints error message
2889 * and does not set the parameter if invalid.
2890 *
2891 * Returns:
2892 * zero on success
2893 * -EINVAL if val is invalid
2894 **/
2895#define lpfc_param_set(attr, default, minval, maxval)	\
2896static int \
2897lpfc_##attr##_set(struct lpfc_hba *phba, uint val) \
2898{ \
2899	if (lpfc_rangecheck(val, minval, maxval)) {\
2900		lpfc_printf_log(phba, KERN_ERR, LOG_INIT, \
2901			"3052 lpfc_" #attr " changed from %d to %d\n", \
2902			phba->cfg_##attr, val); \
2903		phba->cfg_##attr = val;\
2904		return 0;\
2905	}\
2906	lpfc_printf_log(phba, KERN_ERR, LOG_INIT, \
2907			"0450 lpfc_"#attr" attribute cannot be set to %d, "\
2908			"allowed range is ["#minval", "#maxval"]\n", val); \
2909	return -EINVAL;\
2910}
2911
2912/*
2913 * lpfc_param_store - Set a vport attribute value
2914 *
2915 * Description:
2916 * Macro that given an attr e.g. hba_queue_depth expands
2917 * into a function with the name lpfc_hba_queue_depth_store.
2918 *
2919 * lpfc_##attr##_store: Set an sttribute value.
2920 * @dev: class device that is converted into a Scsi_host.
2921 * @attr: device attribute, not used.
2922 * @buf: contains the attribute value in ascii.
2923 * @count: not used.
2924 *
2925 * Description:
2926 * Convert the ascii text number to an integer, then
2927 * use the lpfc_##attr##_set function to set the value.
2928 *
2929 * Returns:
2930 * -EINVAL if val is invalid or lpfc_##attr##_set() fails
2931 * length of buffer upon success.
2932 **/
2933#define lpfc_param_store(attr)	\
2934static ssize_t \
2935lpfc_##attr##_store(struct device *dev, struct device_attribute *attr, \
2936		    const char *buf, size_t count) \
2937{ \
2938	struct Scsi_Host  *shost = class_to_shost(dev);\
2939	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;\
2940	struct lpfc_hba   *phba = vport->phba;\
2941	uint val = 0;\
2942	if (!isdigit(buf[0]))\
2943		return -EINVAL;\
2944	if (sscanf(buf, "%i", &val) != 1)\
2945		return -EINVAL;\
2946	if (lpfc_##attr##_set(phba, val) == 0) \
2947		return strlen(buf);\
2948	else \
2949		return -EINVAL;\
2950}
2951
2952/*
2953 * lpfc_vport_param_show - Return decimal formatted cfg attribute value
2954 *
2955 * Description:
2956 * Macro that given an attr e.g. hba_queue_depth expands
2957 * into a function with the name lpfc_hba_queue_depth_show
2958 *
2959 * lpfc_##attr##_show: prints the attribute value in decimal.
2960 * @dev: class device that is converted into a Scsi_host.
2961 * @attr: device attribute, not used.
2962 * @buf: on return contains the attribute value in decimal.
2963 *
2964 * Returns: length of formatted string.
2965 **/
2966#define lpfc_vport_param_show(attr)	\
2967static ssize_t \
2968lpfc_##attr##_show(struct device *dev, struct device_attribute *attr, \
2969		   char *buf) \
2970{ \
2971	struct Scsi_Host  *shost = class_to_shost(dev);\
2972	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;\
2973	return scnprintf(buf, PAGE_SIZE, "%d\n", vport->cfg_##attr);\
2974}
2975
2976/*
2977 * lpfc_vport_param_hex_show - Return hex formatted attribute value
2978 *
2979 * Description:
2980 * Macro that given an attr e.g.
2981 * hba_queue_depth expands into a function with the name
2982 * lpfc_hba_queue_depth_show
2983 *
2984 * lpfc_##attr##_show: prints the attribute value in hexadecimal.
2985 * @dev: class device that is converted into a Scsi_host.
2986 * @attr: device attribute, not used.
2987 * @buf: on return contains the attribute value in hexadecimal.
2988 *
2989 * Returns: length of formatted string.
2990 **/
2991#define lpfc_vport_param_hex_show(attr)	\
2992static ssize_t \
2993lpfc_##attr##_show(struct device *dev, struct device_attribute *attr, \
2994		   char *buf) \
2995{ \
2996	struct Scsi_Host  *shost = class_to_shost(dev);\
2997	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;\
2998	return scnprintf(buf, PAGE_SIZE, "%#x\n", vport->cfg_##attr);\
2999}
3000
3001/*
3002 * lpfc_vport_param_init - Initialize a vport cfg attribute
3003 *
3004 * Description:
3005 * Macro that given an attr e.g. hba_queue_depth expands
3006 * into a function with the name lpfc_hba_queue_depth_init. The macro also
3007 * takes a default argument, a minimum and maximum argument.
3008 *
3009 * lpfc_##attr##_init: validates the min and max values then sets the
3010 * adapter config field accordingly, or uses the default if out of range
3011 * and prints an error message.
3012 * @phba: pointer to the adapter structure.
3013 * @val: integer attribute value.
3014 *
3015 * Returns:
3016 * zero on success
3017 * -EINVAL if default used
3018 **/
3019#define lpfc_vport_param_init(attr, default, minval, maxval)	\
3020static int \
3021lpfc_##attr##_init(struct lpfc_vport *vport, uint val) \
3022{ \
3023	if (lpfc_rangecheck(val, minval, maxval)) {\
3024		vport->cfg_##attr = val;\
3025		return 0;\
3026	}\
3027	lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT, \
3028			 "0423 lpfc_"#attr" attribute cannot be set to %d, "\
3029			 "allowed range is ["#minval", "#maxval"]\n", val); \
3030	vport->cfg_##attr = default;\
3031	return -EINVAL;\
3032}
3033
3034/*
3035 * lpfc_vport_param_set - Set a vport cfg attribute
3036 *
3037 * Description:
3038 * Macro that given an attr e.g. hba_queue_depth expands
3039 * into a function with the name lpfc_hba_queue_depth_set
3040 *
3041 * lpfc_##attr##_set: validates the min and max values then sets the
3042 * adapter config field if in the valid range. prints error message
3043 * and does not set the parameter if invalid.
3044 * @phba: pointer to the adapter structure.
3045 * @val:	integer attribute value.
3046 *
3047 * Returns:
3048 * zero on success
3049 * -EINVAL if val is invalid
3050 **/
3051#define lpfc_vport_param_set(attr, default, minval, maxval)	\
3052static int \
3053lpfc_##attr##_set(struct lpfc_vport *vport, uint val) \
3054{ \
3055	if (lpfc_rangecheck(val, minval, maxval)) {\
3056		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT, \
3057			"3053 lpfc_" #attr \
3058			" changed from %d (x%x) to %d (x%x)\n", \
3059			vport->cfg_##attr, vport->cfg_##attr, \
3060			val, val); \
3061		vport->cfg_##attr = val;\
3062		return 0;\
3063	}\
3064	lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT, \
3065			 "0424 lpfc_"#attr" attribute cannot be set to %d, "\
3066			 "allowed range is ["#minval", "#maxval"]\n", val); \
3067	return -EINVAL;\
3068}
3069
3070/*
3071 * lpfc_vport_param_store - Set a vport attribute
3072 *
3073 * Description:
3074 * Macro that given an attr e.g. hba_queue_depth
3075 * expands into a function with the name lpfc_hba_queue_depth_store
3076 *
3077 * lpfc_##attr##_store: convert the ascii text number to an integer, then
3078 * use the lpfc_##attr##_set function to set the value.
3079 * @cdev: class device that is converted into a Scsi_host.
3080 * @buf:	contains the attribute value in decimal.
3081 * @count: not used.
3082 *
3083 * Returns:
3084 * -EINVAL if val is invalid or lpfc_##attr##_set() fails
3085 * length of buffer upon success.
3086 **/
3087#define lpfc_vport_param_store(attr)	\
3088static ssize_t \
3089lpfc_##attr##_store(struct device *dev, struct device_attribute *attr, \
3090		    const char *buf, size_t count) \
3091{ \
3092	struct Scsi_Host  *shost = class_to_shost(dev);\
3093	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;\
3094	uint val = 0;\
3095	if (!isdigit(buf[0]))\
3096		return -EINVAL;\
3097	if (sscanf(buf, "%i", &val) != 1)\
3098		return -EINVAL;\
3099	if (lpfc_##attr##_set(vport, val) == 0) \
3100		return strlen(buf);\
3101	else \
3102		return -EINVAL;\
3103}
3104
3105
3106static DEVICE_ATTR(nvme_info, 0444, lpfc_nvme_info_show, NULL);
3107static DEVICE_ATTR(scsi_stat, 0444, lpfc_scsi_stat_show, NULL);
3108static DEVICE_ATTR(bg_info, S_IRUGO, lpfc_bg_info_show, NULL);
3109static DEVICE_ATTR(bg_guard_err, S_IRUGO, lpfc_bg_guard_err_show, NULL);
3110static DEVICE_ATTR(bg_apptag_err, S_IRUGO, lpfc_bg_apptag_err_show, NULL);
3111static DEVICE_ATTR(bg_reftag_err, S_IRUGO, lpfc_bg_reftag_err_show, NULL);
3112static DEVICE_ATTR(info, S_IRUGO, lpfc_info_show, NULL);
3113static DEVICE_ATTR(serialnum, S_IRUGO, lpfc_serialnum_show, NULL);
3114static DEVICE_ATTR(modeldesc, S_IRUGO, lpfc_modeldesc_show, NULL);
3115static DEVICE_ATTR(modelname, S_IRUGO, lpfc_modelname_show, NULL);
3116static DEVICE_ATTR(programtype, S_IRUGO, lpfc_programtype_show, NULL);
3117static DEVICE_ATTR(portnum, S_IRUGO, lpfc_vportnum_show, NULL);
3118static DEVICE_ATTR(fwrev, S_IRUGO, lpfc_fwrev_show, NULL);
3119static DEVICE_ATTR(hdw, S_IRUGO, lpfc_hdw_show, NULL);
3120static DEVICE_ATTR(link_state, S_IRUGO | S_IWUSR, lpfc_link_state_show,
3121		lpfc_link_state_store);
3122static DEVICE_ATTR(option_rom_version, S_IRUGO,
3123		   lpfc_option_rom_version_show, NULL);
3124static DEVICE_ATTR(num_discovered_ports, S_IRUGO,
3125		   lpfc_num_discovered_ports_show, NULL);
3126static DEVICE_ATTR(nport_evt_cnt, S_IRUGO, lpfc_nport_evt_cnt_show, NULL);
3127static DEVICE_ATTR_RO(lpfc_drvr_version);
3128static DEVICE_ATTR_RO(lpfc_enable_fip);
3129static DEVICE_ATTR(board_mode, S_IRUGO | S_IWUSR,
3130		   lpfc_board_mode_show, lpfc_board_mode_store);
3131static DEVICE_ATTR_RO(lpfc_xcvr_data);
3132static DEVICE_ATTR(issue_reset, S_IWUSR, NULL, lpfc_issue_reset);
3133static DEVICE_ATTR(max_vpi, S_IRUGO, lpfc_max_vpi_show, NULL);
3134static DEVICE_ATTR(used_vpi, S_IRUGO, lpfc_used_vpi_show, NULL);
3135static DEVICE_ATTR(max_rpi, S_IRUGO, lpfc_max_rpi_show, NULL);
3136static DEVICE_ATTR(used_rpi, S_IRUGO, lpfc_used_rpi_show, NULL);
3137static DEVICE_ATTR(max_xri, S_IRUGO, lpfc_max_xri_show, NULL);
3138static DEVICE_ATTR(used_xri, S_IRUGO, lpfc_used_xri_show, NULL);
3139static DEVICE_ATTR(npiv_info, S_IRUGO, lpfc_npiv_info_show, NULL);
3140static DEVICE_ATTR_RO(lpfc_temp_sensor);
3141static DEVICE_ATTR_RO(lpfc_sriov_hw_max_virtfn);
3142static DEVICE_ATTR(protocol, S_IRUGO, lpfc_sli4_protocol_show, NULL);
3143static DEVICE_ATTR(lpfc_xlane_supported, S_IRUGO, lpfc_oas_supported_show,
3144		   NULL);
3145static DEVICE_ATTR(cmf_info, 0444, lpfc_cmf_info_show, NULL);
3146static DEVICE_ATTR_RO(lpfc_vmid_info);
3147
3148#define WWN_SZ 8
3149/**
3150 * lpfc_wwn_set - Convert string to the 8 byte WWN value.
3151 * @buf: WWN string.
3152 * @cnt: Length of string.
3153 * @wwn: Array to receive converted wwn value.
3154 *
3155 * Returns:
3156 * -EINVAL if the buffer does not contain a valid wwn
3157 * 0 success
3158 **/
3159static size_t
3160lpfc_wwn_set(const char *buf, size_t cnt, char wwn[])
3161{
3162	unsigned int i, j;
3163
3164	/* Count may include a LF at end of string */
3165	if (buf[cnt-1] == '\n')
3166		cnt--;
3167
3168	if ((cnt < 16) || (cnt > 18) || ((cnt == 17) && (*buf++ != 'x')) ||
3169	    ((cnt == 18) && ((*buf++ != '0') || (*buf++ != 'x'))))
3170		return -EINVAL;
3171
3172	memset(wwn, 0, WWN_SZ);
3173
3174	/* Validate and store the new name */
3175	for (i = 0, j = 0; i < 16; i++) {
3176		if ((*buf >= 'a') && (*buf <= 'f'))
3177			j = ((j << 4) | ((*buf++ - 'a') + 10));
3178		else if ((*buf >= 'A') && (*buf <= 'F'))
3179			j = ((j << 4) | ((*buf++ - 'A') + 10));
3180		else if ((*buf >= '0') && (*buf <= '9'))
3181			j = ((j << 4) | (*buf++ - '0'));
3182		else
3183			return -EINVAL;
3184		if (i % 2) {
3185			wwn[i/2] = j & 0xff;
3186			j = 0;
3187		}
3188	}
3189	return 0;
3190}
3191
3192
3193/**
3194 * lpfc_oas_tgt_show - Return wwpn of target whose luns maybe enabled for
3195 *		      Optimized Access Storage (OAS) operations.
3196 * @dev: class device that is converted into a Scsi_host.
3197 * @attr: device attribute, not used.
3198 * @buf: buffer for passing information.
3199 *
3200 * Returns:
3201 * value of count
3202 **/
3203static ssize_t
3204lpfc_oas_tgt_show(struct device *dev, struct device_attribute *attr,
3205		  char *buf)
3206{
3207	struct Scsi_Host *shost = class_to_shost(dev);
3208	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
3209
3210	return scnprintf(buf, PAGE_SIZE, "0x%llx\n",
3211			wwn_to_u64(phba->cfg_oas_tgt_wwpn));
3212}
3213
3214/**
3215 * lpfc_oas_tgt_store - Store wwpn of target whose luns maybe enabled for
3216 *		      Optimized Access Storage (OAS) operations.
3217 * @dev: class device that is converted into a Scsi_host.
3218 * @attr: device attribute, not used.
3219 * @buf: buffer for passing information.
3220 * @count: Size of the data buffer.
3221 *
3222 * Returns:
3223 * -EINVAL count is invalid, invalid wwpn byte invalid
3224 * -EPERM oas is not supported by hba
3225 * value of count on success
3226 **/
3227static ssize_t
3228lpfc_oas_tgt_store(struct device *dev, struct device_attribute *attr,
3229		   const char *buf, size_t count)
3230{
3231	struct Scsi_Host *shost = class_to_shost(dev);
3232	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
3233	unsigned int cnt = count;
3234	uint8_t wwpn[WWN_SZ];
3235	int rc;
3236
3237	if (!phba->cfg_fof)
3238		return -EPERM;
3239
3240	/* count may include a LF at end of string */
3241	if (buf[cnt-1] == '\n')
3242		cnt--;
3243
3244	rc = lpfc_wwn_set(buf, cnt, wwpn);
3245	if (rc)
3246		return rc;
3247
3248	memcpy(phba->cfg_oas_tgt_wwpn, wwpn, (8 * sizeof(uint8_t)));
3249	memcpy(phba->sli4_hba.oas_next_tgt_wwpn, wwpn, (8 * sizeof(uint8_t)));
3250	if (wwn_to_u64(wwpn) == 0)
3251		phba->cfg_oas_flags |= OAS_FIND_ANY_TARGET;
3252	else
3253		phba->cfg_oas_flags &= ~OAS_FIND_ANY_TARGET;
3254	phba->cfg_oas_flags &= ~OAS_LUN_VALID;
3255	phba->sli4_hba.oas_next_lun = FIND_FIRST_OAS_LUN;
3256	return count;
3257}
3258static DEVICE_ATTR(lpfc_xlane_tgt, S_IRUGO | S_IWUSR,
3259		   lpfc_oas_tgt_show, lpfc_oas_tgt_store);
3260
3261/**
3262 * lpfc_oas_priority_show - Return wwpn of target whose luns maybe enabled for
3263 *		      Optimized Access Storage (OAS) operations.
3264 * @dev: class device that is converted into a Scsi_host.
3265 * @attr: device attribute, not used.
3266 * @buf: buffer for passing information.
3267 *
3268 * Returns:
3269 * value of count
3270 **/
3271static ssize_t
3272lpfc_oas_priority_show(struct device *dev, struct device_attribute *attr,
3273		       char *buf)
3274{
3275	struct Scsi_Host *shost = class_to_shost(dev);
3276	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
3277
3278	return scnprintf(buf, PAGE_SIZE, "%d\n", phba->cfg_oas_priority);
3279}
3280
3281/**
3282 * lpfc_oas_priority_store - Store wwpn of target whose luns maybe enabled for
3283 *		      Optimized Access Storage (OAS) operations.
3284 * @dev: class device that is converted into a Scsi_host.
3285 * @attr: device attribute, not used.
3286 * @buf: buffer for passing information.
3287 * @count: Size of the data buffer.
3288 *
3289 * Returns:
3290 * -EINVAL count is invalid, invalid wwpn byte invalid
3291 * -EPERM oas is not supported by hba
3292 * value of count on success
3293 **/
3294static ssize_t
3295lpfc_oas_priority_store(struct device *dev, struct device_attribute *attr,
3296			const char *buf, size_t count)
3297{
3298	struct Scsi_Host *shost = class_to_shost(dev);
3299	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
3300	unsigned int cnt = count;
3301	unsigned long val;
3302	int ret;
3303
3304	if (!phba->cfg_fof)
3305		return -EPERM;
3306
3307	/* count may include a LF at end of string */
3308	if (buf[cnt-1] == '\n')
3309		cnt--;
3310
3311	ret = kstrtoul(buf, 0, &val);
3312	if (ret || (val > 0x7f))
3313		return -EINVAL;
3314
3315	if (val)
3316		phba->cfg_oas_priority = (uint8_t)val;
3317	else
3318		phba->cfg_oas_priority = phba->cfg_XLanePriority;
3319	return count;
3320}
3321static DEVICE_ATTR(lpfc_xlane_priority, S_IRUGO | S_IWUSR,
3322		   lpfc_oas_priority_show, lpfc_oas_priority_store);
3323
3324/**
3325 * lpfc_oas_vpt_show - Return wwpn of vport whose targets maybe enabled
3326 *		      for Optimized Access Storage (OAS) operations.
3327 * @dev: class device that is converted into a Scsi_host.
3328 * @attr: device attribute, not used.
3329 * @buf: buffer for passing information.
3330 *
3331 * Returns:
3332 * value of count on success
3333 **/
3334static ssize_t
3335lpfc_oas_vpt_show(struct device *dev, struct device_attribute *attr,
3336		  char *buf)
3337{
3338	struct Scsi_Host *shost = class_to_shost(dev);
3339	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
3340
3341	return scnprintf(buf, PAGE_SIZE, "0x%llx\n",
3342			wwn_to_u64(phba->cfg_oas_vpt_wwpn));
3343}
3344
3345/**
3346 * lpfc_oas_vpt_store - Store wwpn of vport whose targets maybe enabled
3347 *		      for Optimized Access Storage (OAS) operations.
3348 * @dev: class device that is converted into a Scsi_host.
3349 * @attr: device attribute, not used.
3350 * @buf: buffer for passing information.
3351 * @count: Size of the data buffer.
3352 *
3353 * Returns:
3354 * -EINVAL count is invalid, invalid wwpn byte invalid
3355 * -EPERM oas is not supported by hba
3356 * value of count on success
3357 **/
3358static ssize_t
3359lpfc_oas_vpt_store(struct device *dev, struct device_attribute *attr,
3360		   const char *buf, size_t count)
3361{
3362	struct Scsi_Host *shost = class_to_shost(dev);
3363	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
3364	unsigned int cnt = count;
3365	uint8_t wwpn[WWN_SZ];
3366	int rc;
3367
3368	if (!phba->cfg_fof)
3369		return -EPERM;
3370
3371	/* count may include a LF at end of string */
3372	if (buf[cnt-1] == '\n')
3373		cnt--;
3374
3375	rc = lpfc_wwn_set(buf, cnt, wwpn);
3376	if (rc)
3377		return rc;
3378
3379	memcpy(phba->cfg_oas_vpt_wwpn, wwpn, (8 * sizeof(uint8_t)));
3380	memcpy(phba->sli4_hba.oas_next_vpt_wwpn, wwpn, (8 * sizeof(uint8_t)));
3381	if (wwn_to_u64(wwpn) == 0)
3382		phba->cfg_oas_flags |= OAS_FIND_ANY_VPORT;
3383	else
3384		phba->cfg_oas_flags &= ~OAS_FIND_ANY_VPORT;
3385	phba->cfg_oas_flags &= ~OAS_LUN_VALID;
3386	if (phba->cfg_oas_priority == 0)
3387		phba->cfg_oas_priority = phba->cfg_XLanePriority;
3388	phba->sli4_hba.oas_next_lun = FIND_FIRST_OAS_LUN;
3389	return count;
3390}
3391static DEVICE_ATTR(lpfc_xlane_vpt, S_IRUGO | S_IWUSR,
3392		   lpfc_oas_vpt_show, lpfc_oas_vpt_store);
3393
3394/**
3395 * lpfc_oas_lun_state_show - Return the current state (enabled or disabled)
3396 *			    of whether luns will be enabled or disabled
3397 *			    for Optimized Access Storage (OAS) operations.
3398 * @dev: class device that is converted into a Scsi_host.
3399 * @attr: device attribute, not used.
3400 * @buf: buffer for passing information.
3401 *
3402 * Returns:
3403 * size of formatted string.
3404 **/
3405static ssize_t
3406lpfc_oas_lun_state_show(struct device *dev, struct device_attribute *attr,
3407			char *buf)
3408{
3409	struct Scsi_Host *shost = class_to_shost(dev);
3410	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
3411
3412	return scnprintf(buf, PAGE_SIZE, "%d\n", phba->cfg_oas_lun_state);
3413}
3414
3415/**
3416 * lpfc_oas_lun_state_store - Store the state (enabled or disabled)
3417 *			    of whether luns will be enabled or disabled
3418 *			    for Optimized Access Storage (OAS) operations.
3419 * @dev: class device that is converted into a Scsi_host.
3420 * @attr: device attribute, not used.
3421 * @buf: buffer for passing information.
3422 * @count: Size of the data buffer.
3423 *
3424 * Returns:
3425 * -EINVAL count is invalid, invalid wwpn byte invalid
3426 * -EPERM oas is not supported by hba
3427 * value of count on success
3428 **/
3429static ssize_t
3430lpfc_oas_lun_state_store(struct device *dev, struct device_attribute *attr,
3431			 const char *buf, size_t count)
3432{
3433	struct Scsi_Host *shost = class_to_shost(dev);
3434	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
3435	int val = 0;
3436
3437	if (!phba->cfg_fof)
3438		return -EPERM;
3439
3440	if (!isdigit(buf[0]))
3441		return -EINVAL;
3442
3443	if (sscanf(buf, "%i", &val) != 1)
3444		return -EINVAL;
3445
3446	if ((val != 0) && (val != 1))
3447		return -EINVAL;
3448
3449	phba->cfg_oas_lun_state = val;
3450	return strlen(buf);
3451}
3452static DEVICE_ATTR(lpfc_xlane_lun_state, S_IRUGO | S_IWUSR,
3453		   lpfc_oas_lun_state_show, lpfc_oas_lun_state_store);
3454
3455/**
3456 * lpfc_oas_lun_status_show - Return the status of the Optimized Access
3457 *                          Storage (OAS) lun returned by the
3458 *                          lpfc_oas_lun_show function.
3459 * @dev: class device that is converted into a Scsi_host.
3460 * @attr: device attribute, not used.
3461 * @buf: buffer for passing information.
3462 *
3463 * Returns:
3464 * size of formatted string.
3465 **/
3466static ssize_t
3467lpfc_oas_lun_status_show(struct device *dev, struct device_attribute *attr,
3468			 char *buf)
3469{
3470	struct Scsi_Host *shost = class_to_shost(dev);
3471	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
3472
3473	if (!(phba->cfg_oas_flags & OAS_LUN_VALID))
3474		return -EFAULT;
3475
3476	return scnprintf(buf, PAGE_SIZE, "%d\n", phba->cfg_oas_lun_status);
3477}
3478static DEVICE_ATTR(lpfc_xlane_lun_status, S_IRUGO,
3479		   lpfc_oas_lun_status_show, NULL);
3480
3481
3482/**
3483 * lpfc_oas_lun_state_set - enable or disable a lun for Optimized Access Storage
3484 *			   (OAS) operations.
3485 * @phba: lpfc_hba pointer.
3486 * @vpt_wwpn: wwpn of the vport associated with the returned lun
3487 * @tgt_wwpn: wwpn of the target associated with the returned lun
3488 * @lun: the fc lun for setting oas state.
3489 * @oas_state: the oas state to be set to the lun.
3490 * @pri: priority
3491 *
3492 * Returns:
3493 * SUCCESS : 0
3494 * -EPERM OAS is not enabled or not supported by this port.
3495 *
3496 */
3497static size_t
3498lpfc_oas_lun_state_set(struct lpfc_hba *phba, uint8_t vpt_wwpn[],
3499		       uint8_t tgt_wwpn[], uint64_t lun,
3500		       uint32_t oas_state, uint8_t pri)
3501{
3502
3503	int rc = 0;
3504
3505	if (!phba->cfg_fof)
3506		return -EPERM;
3507
3508	if (oas_state) {
3509		if (!lpfc_enable_oas_lun(phba, (struct lpfc_name *)vpt_wwpn,
3510					 (struct lpfc_name *)tgt_wwpn,
3511					 lun, pri))
3512			rc = -ENOMEM;
3513	} else {
3514		lpfc_disable_oas_lun(phba, (struct lpfc_name *)vpt_wwpn,
3515				     (struct lpfc_name *)tgt_wwpn, lun, pri);
3516	}
3517	return rc;
3518
3519}
3520
3521/**
3522 * lpfc_oas_lun_get_next - get the next lun that has been enabled for Optimized
3523 *			  Access Storage (OAS) operations.
3524 * @phba: lpfc_hba pointer.
3525 * @vpt_wwpn: wwpn of the vport associated with the returned lun
3526 * @tgt_wwpn: wwpn of the target associated with the returned lun
3527 * @lun_status: status of the lun returned lun
3528 * @lun_pri: priority of the lun returned lun
3529 *
3530 * Returns the first or next lun enabled for OAS operations for the vport/target
3531 * specified.  If a lun is found, its vport wwpn, target wwpn and status is
3532 * returned.  If the lun is not found, NOT_OAS_ENABLED_LUN is returned.
3533 *
3534 * Return:
3535 * lun that is OAS enabled for the vport/target
3536 * NOT_OAS_ENABLED_LUN when no oas enabled lun found.
3537 */
3538static uint64_t
3539lpfc_oas_lun_get_next(struct lpfc_hba *phba, uint8_t vpt_wwpn[],
3540		      uint8_t tgt_wwpn[], uint32_t *lun_status,
3541		      uint32_t *lun_pri)
3542{
3543	uint64_t found_lun;
3544
3545	if (unlikely(!phba) || !vpt_wwpn || !tgt_wwpn)
3546		return NOT_OAS_ENABLED_LUN;
3547	if (lpfc_find_next_oas_lun(phba, (struct lpfc_name *)
3548				   phba->sli4_hba.oas_next_vpt_wwpn,
3549				   (struct lpfc_name *)
3550				   phba->sli4_hba.oas_next_tgt_wwpn,
3551				   &phba->sli4_hba.oas_next_lun,
3552				   (struct lpfc_name *)vpt_wwpn,
3553				   (struct lpfc_name *)tgt_wwpn,
3554				   &found_lun, lun_status, lun_pri))
3555		return found_lun;
3556	else
3557		return NOT_OAS_ENABLED_LUN;
3558}
3559
3560/**
3561 * lpfc_oas_lun_state_change - enable/disable a lun for OAS operations
3562 * @phba: lpfc_hba pointer.
3563 * @vpt_wwpn: vport wwpn by reference.
3564 * @tgt_wwpn: target wwpn by reference.
3565 * @lun: the fc lun for setting oas state.
3566 * @oas_state: the oas state to be set to the oas_lun.
3567 * @pri: priority
3568 *
3569 * This routine enables (OAS_LUN_ENABLE) or disables (OAS_LUN_DISABLE)
3570 * a lun for OAS operations.
3571 *
3572 * Return:
3573 * SUCCESS: 0
3574 * -ENOMEM: failed to enable an lun for OAS operations
3575 * -EPERM: OAS is not enabled
3576 */
3577static ssize_t
3578lpfc_oas_lun_state_change(struct lpfc_hba *phba, uint8_t vpt_wwpn[],
3579			  uint8_t tgt_wwpn[], uint64_t lun,
3580			  uint32_t oas_state, uint8_t pri)
3581{
3582
3583	int rc;
3584
3585	rc = lpfc_oas_lun_state_set(phba, vpt_wwpn, tgt_wwpn, lun,
3586				    oas_state, pri);
3587	return rc;
3588}
3589
3590/**
3591 * lpfc_oas_lun_show - Return oas enabled luns from a chosen target
3592 * @dev: class device that is converted into a Scsi_host.
3593 * @attr: device attribute, not used.
3594 * @buf: buffer for passing information.
3595 *
3596 * This routine returns a lun enabled for OAS each time the function
3597 * is called.
3598 *
3599 * Returns:
3600 * SUCCESS: size of formatted string.
3601 * -EFAULT: target or vport wwpn was not set properly.
3602 * -EPERM: oas is not enabled.
3603 **/
3604static ssize_t
3605lpfc_oas_lun_show(struct device *dev, struct device_attribute *attr,
3606		  char *buf)
3607{
3608	struct Scsi_Host *shost = class_to_shost(dev);
3609	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
3610
3611	uint64_t oas_lun;
3612	int len = 0;
3613
3614	if (!phba->cfg_fof)
3615		return -EPERM;
3616
3617	if (wwn_to_u64(phba->cfg_oas_vpt_wwpn) == 0)
3618		if (!(phba->cfg_oas_flags & OAS_FIND_ANY_VPORT))
3619			return -EFAULT;
3620
3621	if (wwn_to_u64(phba->cfg_oas_tgt_wwpn) == 0)
3622		if (!(phba->cfg_oas_flags & OAS_FIND_ANY_TARGET))
3623			return -EFAULT;
3624
3625	oas_lun = lpfc_oas_lun_get_next(phba, phba->cfg_oas_vpt_wwpn,
3626					phba->cfg_oas_tgt_wwpn,
3627					&phba->cfg_oas_lun_status,
3628					&phba->cfg_oas_priority);
3629	if (oas_lun != NOT_OAS_ENABLED_LUN)
3630		phba->cfg_oas_flags |= OAS_LUN_VALID;
3631
3632	len += scnprintf(buf + len, PAGE_SIZE-len, "0x%llx", oas_lun);
3633
3634	return len;
3635}
3636
3637/**
3638 * lpfc_oas_lun_store - Sets the OAS state for lun
3639 * @dev: class device that is converted into a Scsi_host.
3640 * @attr: device attribute, not used.
3641 * @buf: buffer for passing information.
3642 * @count: size of the formatting string
3643 *
3644 * This function sets the OAS state for lun.  Before this function is called,
3645 * the vport wwpn, target wwpn, and oas state need to be set.
3646 *
3647 * Returns:
3648 * SUCCESS: size of formatted string.
3649 * -EFAULT: target or vport wwpn was not set properly.
3650 * -EPERM: oas is not enabled.
3651 * size of formatted string.
3652 **/
3653static ssize_t
3654lpfc_oas_lun_store(struct device *dev, struct device_attribute *attr,
3655		   const char *buf, size_t count)
3656{
3657	struct Scsi_Host *shost = class_to_shost(dev);
3658	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
3659	uint64_t scsi_lun;
3660	uint32_t pri;
3661	ssize_t rc;
3662
3663	if (!phba->cfg_fof)
3664		return -EPERM;
3665
3666	if (wwn_to_u64(phba->cfg_oas_vpt_wwpn) == 0)
3667		return -EFAULT;
3668
3669	if (wwn_to_u64(phba->cfg_oas_tgt_wwpn) == 0)
3670		return -EFAULT;
3671
3672	if (!isdigit(buf[0]))
3673		return -EINVAL;
3674
3675	if (sscanf(buf, "0x%llx", &scsi_lun) != 1)
3676		return -EINVAL;
3677
3678	pri = phba->cfg_oas_priority;
3679	if (pri == 0)
3680		pri = phba->cfg_XLanePriority;
3681
3682	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3683			"3372 Try to set vport 0x%llx target 0x%llx lun:0x%llx "
3684			"priority 0x%x with oas state %d\n",
3685			wwn_to_u64(phba->cfg_oas_vpt_wwpn),
3686			wwn_to_u64(phba->cfg_oas_tgt_wwpn), scsi_lun,
3687			pri, phba->cfg_oas_lun_state);
3688
3689	rc = lpfc_oas_lun_state_change(phba, phba->cfg_oas_vpt_wwpn,
3690				       phba->cfg_oas_tgt_wwpn, scsi_lun,
3691				       phba->cfg_oas_lun_state, pri);
3692	if (rc)
3693		return rc;
3694
3695	return count;
3696}
3697static DEVICE_ATTR(lpfc_xlane_lun, S_IRUGO | S_IWUSR,
3698		   lpfc_oas_lun_show, lpfc_oas_lun_store);
3699
3700int lpfc_enable_nvmet_cnt;
3701unsigned long long lpfc_enable_nvmet[LPFC_NVMET_MAX_PORTS] = {
3702	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3703	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
3704module_param_array(lpfc_enable_nvmet, ullong, &lpfc_enable_nvmet_cnt, 0444);
3705MODULE_PARM_DESC(lpfc_enable_nvmet, "Enable HBA port(s) WWPN as a NVME Target");
3706
3707static int lpfc_poll = 0;
3708module_param(lpfc_poll, int, S_IRUGO);
3709MODULE_PARM_DESC(lpfc_poll, "FCP ring polling mode control:"
3710		 " 0 - none,"
3711		 " 1 - poll with interrupts enabled"
3712		 " 3 - poll and disable FCP ring interrupts");
3713
3714static DEVICE_ATTR_RW(lpfc_poll);
3715
3716int lpfc_no_hba_reset_cnt;
3717unsigned long lpfc_no_hba_reset[MAX_HBAS_NO_RESET] = {
3718	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
3719module_param_array(lpfc_no_hba_reset, ulong, &lpfc_no_hba_reset_cnt, 0444);
3720MODULE_PARM_DESC(lpfc_no_hba_reset, "WWPN of HBAs that should not be reset");
3721
3722LPFC_ATTR(sli_mode, 3, 3, 3,
3723	"SLI mode selector: 3 - select SLI-3");
3724
3725LPFC_ATTR_R(enable_npiv, 1, 0, 1,
3726	"Enable NPIV functionality");
3727
3728LPFC_ATTR_R(fcf_failover_policy, 1, 1, 2,
3729	"FCF Fast failover=1 Priority failover=2");
3730
3731/*
3732 * lpfc_fcp_wait_abts_rsp: Modifies criteria for reporting completion of
3733 * aborted IO.
3734 * The range is [0,1]. Default value is 0
3735 *      0, IO completes after ABTS issued (default).
3736 *      1, IO completes after receipt of ABTS response or timeout.
3737 */
3738LPFC_ATTR_R(fcp_wait_abts_rsp, 0, 0, 1, "Wait for FCP ABTS completion");
3739
3740/*
3741# lpfc_enable_rrq: Track XRI/OXID reuse after IO failures
3742#	0x0 = disabled, XRI/OXID use not tracked.
3743#	0x1 = XRI/OXID reuse is timed with ratov, RRQ sent.
3744#	0x2 = XRI/OXID reuse is timed with ratov, No RRQ sent.
3745*/
3746LPFC_ATTR_R(enable_rrq, 2, 0, 2,
3747	"Enable RRQ functionality");
3748
3749/*
3750# lpfc_suppress_link_up:  Bring link up at initialization
3751#            0x0  = bring link up (issue MBX_INIT_LINK)
3752#            0x1  = do NOT bring link up at initialization(MBX_INIT_LINK)
3753#            0x2  = never bring up link
3754# Default value is 0.
3755*/
3756LPFC_ATTR_R(suppress_link_up, LPFC_INITIALIZE_LINK, LPFC_INITIALIZE_LINK,
3757		LPFC_DELAY_INIT_LINK_INDEFINITELY,
3758		"Suppress Link Up at initialization");
3759
3760static ssize_t
3761lpfc_pls_show(struct device *dev, struct device_attribute *attr, char *buf)
3762{
3763	struct Scsi_Host  *shost = class_to_shost(dev);
3764	struct lpfc_hba   *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
3765
3766	return scnprintf(buf, PAGE_SIZE, "%d\n",
3767			 phba->sli4_hba.pc_sli4_params.pls);
3768}
3769static DEVICE_ATTR(pls, 0444,
3770			 lpfc_pls_show, NULL);
3771
3772static ssize_t
3773lpfc_pt_show(struct device *dev, struct device_attribute *attr, char *buf)
3774{
3775	struct Scsi_Host  *shost = class_to_shost(dev);
3776	struct lpfc_hba   *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
3777
3778	return scnprintf(buf, PAGE_SIZE, "%d\n",
3779			 test_bit(HBA_PERSISTENT_TOPO,
3780				  &phba->hba_flag) ? 1 : 0);
3781}
3782static DEVICE_ATTR(pt, 0444,
3783			 lpfc_pt_show, NULL);
3784
3785/*
3786# lpfc_cnt: Number of IOCBs allocated for ELS, CT, and ABTS
3787#       1 - (1024)
3788#       2 - (2048)
3789#       3 - (3072)
3790#       4 - (4096)
3791#       5 - (5120)
3792*/
3793static ssize_t
3794lpfc_iocb_hw_show(struct device *dev, struct device_attribute *attr, char *buf)
3795{
3796	struct Scsi_Host  *shost = class_to_shost(dev);
3797	struct lpfc_hba   *phba = ((struct lpfc_vport *) shost->hostdata)->phba;
3798
3799	return scnprintf(buf, PAGE_SIZE, "%d\n", phba->iocb_max);
3800}
3801
3802static DEVICE_ATTR(iocb_hw, S_IRUGO,
3803			 lpfc_iocb_hw_show, NULL);
3804static ssize_t
3805lpfc_txq_hw_show(struct device *dev, struct device_attribute *attr, char *buf)
3806{
3807	struct Scsi_Host  *shost = class_to_shost(dev);
3808	struct lpfc_hba   *phba = ((struct lpfc_vport *) shost->hostdata)->phba;
3809	struct lpfc_sli_ring *pring = lpfc_phba_elsring(phba);
3810
3811	return scnprintf(buf, PAGE_SIZE, "%d\n",
3812			pring ? pring->txq_max : 0);
3813}
3814
3815static DEVICE_ATTR(txq_hw, S_IRUGO,
3816			 lpfc_txq_hw_show, NULL);
3817static ssize_t
3818lpfc_txcmplq_hw_show(struct device *dev, struct device_attribute *attr,
3819 char *buf)
3820{
3821	struct Scsi_Host  *shost = class_to_shost(dev);
3822	struct lpfc_hba   *phba = ((struct lpfc_vport *) shost->hostdata)->phba;
3823	struct lpfc_sli_ring *pring = lpfc_phba_elsring(phba);
3824
3825	return scnprintf(buf, PAGE_SIZE, "%d\n",
3826			pring ? pring->txcmplq_max : 0);
3827}
3828
3829static DEVICE_ATTR(txcmplq_hw, S_IRUGO,
3830			 lpfc_txcmplq_hw_show, NULL);
3831
3832/*
3833# lpfc_nodev_tmo: If set, it will hold all I/O errors on devices that disappear
3834# until the timer expires. Value range is [0,255]. Default value is 30.
3835*/
3836static int lpfc_nodev_tmo = LPFC_DEF_DEVLOSS_TMO;
3837static int lpfc_devloss_tmo = LPFC_DEF_DEVLOSS_TMO;
3838module_param(lpfc_nodev_tmo, int, 0);
3839MODULE_PARM_DESC(lpfc_nodev_tmo,
3840		 "Seconds driver will hold I/O waiting "
3841		 "for a device to come back");
3842
3843/**
3844 * lpfc_nodev_tmo_show - Return the hba dev loss timeout value
3845 * @dev: class converted to a Scsi_host structure.
3846 * @attr: device attribute, not used.
3847 * @buf: on return contains the dev loss timeout in decimal.
3848 *
3849 * Returns: size of formatted string.
3850 **/
3851static ssize_t
3852lpfc_nodev_tmo_show(struct device *dev, struct device_attribute *attr,
3853		    char *buf)
3854{
3855	struct Scsi_Host  *shost = class_to_shost(dev);
3856	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3857
3858	return scnprintf(buf, PAGE_SIZE, "%d\n",	vport->cfg_devloss_tmo);
3859}
3860
3861/**
3862 * lpfc_nodev_tmo_init - Set the hba nodev timeout value
3863 * @vport: lpfc vport structure pointer.
3864 * @val: contains the nodev timeout value.
3865 *
3866 * Description:
3867 * If the devloss tmo is already set then nodev tmo is set to devloss tmo,
3868 * a kernel error message is printed and zero is returned.
3869 * Else if val is in range then nodev tmo and devloss tmo are set to val.
3870 * Otherwise nodev tmo is set to the default value.
3871 *
3872 * Returns:
3873 * zero if already set or if val is in range
3874 * -EINVAL val out of range
3875 **/
3876static int
3877lpfc_nodev_tmo_init(struct lpfc_vport *vport, int val)
3878{
3879	if (vport->cfg_devloss_tmo != LPFC_DEF_DEVLOSS_TMO) {
3880		vport->cfg_nodev_tmo = vport->cfg_devloss_tmo;
3881		if (val != LPFC_DEF_DEVLOSS_TMO)
3882			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3883					 "0407 Ignoring lpfc_nodev_tmo module "
3884					 "parameter because lpfc_devloss_tmo "
3885					 "is set.\n");
3886		return 0;
3887	}
3888
3889	if (val >= LPFC_MIN_DEVLOSS_TMO && val <= LPFC_MAX_DEVLOSS_TMO) {
3890		vport->cfg_nodev_tmo = val;
3891		vport->cfg_devloss_tmo = val;
3892		return 0;
3893	}
3894	lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3895			 "0400 lpfc_nodev_tmo attribute cannot be set to"
3896			 " %d, allowed range is [%d, %d]\n",
3897			 val, LPFC_MIN_DEVLOSS_TMO, LPFC_MAX_DEVLOSS_TMO);
3898	vport->cfg_nodev_tmo = LPFC_DEF_DEVLOSS_TMO;
3899	return -EINVAL;
3900}
3901
3902/**
3903 * lpfc_update_rport_devloss_tmo - Update dev loss tmo value
3904 * @vport: lpfc vport structure pointer.
3905 *
3906 * Description:
3907 * Update all the ndlp's dev loss tmo with the vport devloss tmo value.
3908 **/
3909static void
3910lpfc_update_rport_devloss_tmo(struct lpfc_vport *vport)
3911{
3912	struct lpfc_nodelist  *ndlp;
3913	unsigned long iflags;
3914#if (IS_ENABLED(CONFIG_NVME_FC))
3915	struct lpfc_nvme_rport *rport;
3916	struct nvme_fc_remote_port *remoteport = NULL;
3917#endif
3918
3919	spin_lock_irqsave(&vport->fc_nodes_list_lock, iflags);
3920	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
3921		if (ndlp->rport)
3922			ndlp->rport->dev_loss_tmo = vport->cfg_devloss_tmo;
3923#if (IS_ENABLED(CONFIG_NVME_FC))
3924		spin_lock(&ndlp->lock);
3925		rport = lpfc_ndlp_get_nrport(ndlp);
3926		if (rport)
3927			remoteport = rport->remoteport;
3928		spin_unlock(&ndlp->lock);
3929		if (rport && remoteport)
3930			nvme_fc_set_remoteport_devloss(remoteport,
3931						       vport->cfg_devloss_tmo);
3932#endif
3933	}
3934	spin_unlock_irqrestore(&vport->fc_nodes_list_lock, iflags);
3935}
3936
3937/**
3938 * lpfc_nodev_tmo_set - Set the vport nodev tmo and devloss tmo values
3939 * @vport: lpfc vport structure pointer.
3940 * @val: contains the tmo value.
3941 *
3942 * Description:
3943 * If the devloss tmo is already set or the vport dev loss tmo has changed
3944 * then a kernel error message is printed and zero is returned.
3945 * Else if val is in range then nodev tmo and devloss tmo are set to val.
3946 * Otherwise nodev tmo is set to the default value.
3947 *
3948 * Returns:
3949 * zero if already set or if val is in range
3950 * -EINVAL val out of range
3951 **/
3952static int
3953lpfc_nodev_tmo_set(struct lpfc_vport *vport, int val)
3954{
3955	if (vport->dev_loss_tmo_changed ||
3956	    (lpfc_devloss_tmo != LPFC_DEF_DEVLOSS_TMO)) {
3957		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3958				 "0401 Ignoring change to lpfc_nodev_tmo "
3959				 "because lpfc_devloss_tmo is set.\n");
3960		return 0;
3961	}
3962	if (val >= LPFC_MIN_DEVLOSS_TMO && val <= LPFC_MAX_DEVLOSS_TMO) {
3963		vport->cfg_nodev_tmo = val;
3964		vport->cfg_devloss_tmo = val;
3965		/*
3966		 * For compat: set the fc_host dev loss so new rports
3967		 * will get the value.
3968		 */
3969		fc_host_dev_loss_tmo(lpfc_shost_from_vport(vport)) = val;
3970		lpfc_update_rport_devloss_tmo(vport);
3971		return 0;
3972	}
3973	lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3974			 "0403 lpfc_nodev_tmo attribute cannot be set to "
3975			 "%d, allowed range is [%d, %d]\n",
3976			 val, LPFC_MIN_DEVLOSS_TMO, LPFC_MAX_DEVLOSS_TMO);
3977	return -EINVAL;
3978}
3979
3980lpfc_vport_param_store(nodev_tmo)
3981
3982static DEVICE_ATTR_RW(lpfc_nodev_tmo);
3983
3984/*
3985# lpfc_devloss_tmo: If set, it will hold all I/O errors on devices that
3986# disappear until the timer expires. Value range is [0,255]. Default
3987# value is 30.
3988*/
3989module_param(lpfc_devloss_tmo, int, S_IRUGO);
3990MODULE_PARM_DESC(lpfc_devloss_tmo,
3991		 "Seconds driver will hold I/O waiting "
3992		 "for a device to come back");
3993lpfc_vport_param_init(devloss_tmo, LPFC_DEF_DEVLOSS_TMO,
3994		      LPFC_MIN_DEVLOSS_TMO, LPFC_MAX_DEVLOSS_TMO)
3995lpfc_vport_param_show(devloss_tmo)
3996
3997/**
3998 * lpfc_devloss_tmo_set - Sets vport nodev tmo, devloss tmo values, changed bit
3999 * @vport: lpfc vport structure pointer.
4000 * @val: contains the tmo value.
4001 *
4002 * Description:
4003 * If val is in a valid range then set the vport nodev tmo,
4004 * devloss tmo, also set the vport dev loss tmo changed flag.
4005 * Else a kernel error message is printed.
4006 *
4007 * Returns:
4008 * zero if val is in range
4009 * -EINVAL val out of range
4010 **/
4011static int
4012lpfc_devloss_tmo_set(struct lpfc_vport *vport, int val)
4013{
4014	if (val >= LPFC_MIN_DEVLOSS_TMO && val <= LPFC_MAX_DEVLOSS_TMO) {
4015		vport->cfg_nodev_tmo = val;
4016		vport->cfg_devloss_tmo = val;
4017		vport->dev_loss_tmo_changed = 1;
4018		fc_host_dev_loss_tmo(lpfc_shost_from_vport(vport)) = val;
4019		lpfc_update_rport_devloss_tmo(vport);
4020		return 0;
4021	}
4022
4023	lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4024			 "0404 lpfc_devloss_tmo attribute cannot be set to "
4025			 "%d, allowed range is [%d, %d]\n",
4026			 val, LPFC_MIN_DEVLOSS_TMO, LPFC_MAX_DEVLOSS_TMO);
4027	return -EINVAL;
4028}
4029
4030lpfc_vport_param_store(devloss_tmo)
4031static DEVICE_ATTR_RW(lpfc_devloss_tmo);
4032
4033/*
4034 * lpfc_suppress_rsp: Enable suppress rsp feature is firmware supports it
4035 * lpfc_suppress_rsp = 0  Disable
4036 * lpfc_suppress_rsp = 1  Enable (default)
4037 *
4038 */
4039LPFC_ATTR_R(suppress_rsp, 1, 0, 1,
4040	    "Enable suppress rsp feature is firmware supports it");
4041
4042/*
4043 * lpfc_nvmet_mrq: Specify number of RQ pairs for processing NVMET cmds
4044 * lpfc_nvmet_mrq = 0  driver will calcualte optimal number of RQ pairs
4045 * lpfc_nvmet_mrq = 1  use a single RQ pair
4046 * lpfc_nvmet_mrq >= 2  use specified RQ pairs for MRQ
4047 *
4048 */
4049LPFC_ATTR_R(nvmet_mrq,
4050	    LPFC_NVMET_MRQ_AUTO, LPFC_NVMET_MRQ_AUTO, LPFC_NVMET_MRQ_MAX,
4051	    "Specify number of RQ pairs for processing NVMET cmds");
4052
4053/*
4054 * lpfc_nvmet_mrq_post: Specify number of RQ buffer to initially post
4055 * to each NVMET RQ. Range 64 to 2048, default is 512.
4056 */
4057LPFC_ATTR_R(nvmet_mrq_post,
4058	    LPFC_NVMET_RQE_DEF_POST, LPFC_NVMET_RQE_MIN_POST,
4059	    LPFC_NVMET_RQE_DEF_COUNT,
4060	    "Specify number of RQ buffers to initially post");
4061
4062/*
4063 * lpfc_enable_fc4_type: Defines what FC4 types are supported.
4064 * Supported Values:  1 - register just FCP
4065 *                    3 - register both FCP and NVME
4066 * Supported values are [1,3]. Default value is 3
4067 */
4068LPFC_ATTR_R(enable_fc4_type, LPFC_DEF_ENBL_FC4_TYPE,
4069	    LPFC_ENABLE_FCP, LPFC_MAX_ENBL_FC4_TYPE,
4070	    "Enable FC4 Protocol support - FCP / NVME");
4071
4072/*
4073# lpfc_log_verbose: Only turn this flag on if you are willing to risk being
4074# deluged with LOTS of information.
4075# You can set a bit mask to record specific types of verbose messages:
4076# See lpfc_logmsh.h for definitions.
4077*/
4078LPFC_VPORT_ATTR_HEX_RW(log_verbose, 0x0, 0x0, 0xffffffff,
4079		       "Verbose logging bit-mask");
4080
4081/*
4082# lpfc_enable_da_id: This turns on the DA_ID CT command that deregisters
4083# objects that have been registered with the nameserver after login.
4084*/
4085LPFC_VPORT_ATTR_R(enable_da_id, 1, 0, 1,
4086		  "Deregister nameserver objects before LOGO");
4087
4088/*
4089# lun_queue_depth:  This parameter is used to limit the number of outstanding
4090# commands per FCP LUN.
4091*/
4092LPFC_VPORT_ATTR_R(lun_queue_depth, 64, 1, 512,
4093		  "Max number of FCP commands we can queue to a specific LUN");
4094
4095/*
4096# tgt_queue_depth:  This parameter is used to limit the number of outstanding
4097# commands per target port. Value range is [10,65535]. Default value is 65535.
4098*/
4099static uint lpfc_tgt_queue_depth = LPFC_MAX_TGT_QDEPTH;
4100module_param(lpfc_tgt_queue_depth, uint, 0444);
4101MODULE_PARM_DESC(lpfc_tgt_queue_depth, "Set max Target queue depth");
4102lpfc_vport_param_show(tgt_queue_depth);
4103lpfc_vport_param_init(tgt_queue_depth, LPFC_MAX_TGT_QDEPTH,
4104		      LPFC_MIN_TGT_QDEPTH, LPFC_MAX_TGT_QDEPTH);
4105
4106/**
4107 * lpfc_tgt_queue_depth_set: Sets an attribute value.
4108 * @vport: lpfc vport structure pointer.
4109 * @val: integer attribute value.
4110 *
4111 * Description: Sets the parameter to the new value.
4112 *
4113 * Returns:
4114 * zero on success
4115 * -EINVAL if val is invalid
4116 */
4117static int
4118lpfc_tgt_queue_depth_set(struct lpfc_vport *vport, uint val)
4119{
4120	struct lpfc_nodelist *ndlp;
4121	unsigned long iflags;
4122
4123	if (!lpfc_rangecheck(val, LPFC_MIN_TGT_QDEPTH, LPFC_MAX_TGT_QDEPTH))
4124		return -EINVAL;
4125
4126	if (val == vport->cfg_tgt_queue_depth)
4127		return 0;
4128
4129	vport->cfg_tgt_queue_depth = val;
4130
4131	/* Next loop thru nodelist and change cmd_qdepth */
4132	spin_lock_irqsave(&vport->fc_nodes_list_lock, iflags);
4133	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp)
4134		ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
4135	spin_unlock_irqrestore(&vport->fc_nodes_list_lock, iflags);
4136	return 0;
4137}
4138
4139lpfc_vport_param_store(tgt_queue_depth);
4140static DEVICE_ATTR_RW(lpfc_tgt_queue_depth);
4141
4142/*
4143# hba_queue_depth:  This parameter is used to limit the number of outstanding
4144# commands per lpfc HBA. Value range is [32,8192]. If this parameter
4145# value is greater than the maximum number of exchanges supported by the HBA,
4146# then maximum number of exchanges supported by the HBA is used to determine
4147# the hba_queue_depth.
4148*/
4149LPFC_ATTR_R(hba_queue_depth, 8192, 32, 8192,
4150	    "Max number of FCP commands we can queue to a lpfc HBA");
4151
4152/*
4153# peer_port_login:  This parameter allows/prevents logins
4154# between peer ports hosted on the same physical port.
4155# When this parameter is set 0 peer ports of same physical port
4156# are not allowed to login to each other.
4157# When this parameter is set 1 peer ports of same physical port
4158# are allowed to login to each other.
4159# Default value of this parameter is 0.
4160*/
4161LPFC_VPORT_ATTR_R(peer_port_login, 0, 0, 1,
4162		  "Allow peer ports on the same physical port to login to each "
4163		  "other.");
4164
4165/*
4166# restrict_login:  This parameter allows/prevents logins
4167# between Virtual Ports and remote initiators.
4168# When this parameter is not set (0) Virtual Ports will accept PLOGIs from
4169# other initiators and will attempt to PLOGI all remote ports.
4170# When this parameter is set (1) Virtual Ports will reject PLOGIs from
4171# remote ports and will not attempt to PLOGI to other initiators.
4172# This parameter does not restrict to the physical port.
4173# This parameter does not restrict logins to Fabric resident remote ports.
4174# Default value of this parameter is 1.
4175*/
4176static int lpfc_restrict_login = 1;
4177module_param(lpfc_restrict_login, int, S_IRUGO);
4178MODULE_PARM_DESC(lpfc_restrict_login,
4179		 "Restrict virtual ports login to remote initiators.");
4180lpfc_vport_param_show(restrict_login);
4181
4182/**
4183 * lpfc_restrict_login_init - Set the vport restrict login flag
4184 * @vport: lpfc vport structure pointer.
4185 * @val: contains the restrict login value.
4186 *
4187 * Description:
4188 * If val is not in a valid range then log a kernel error message and set
4189 * the vport restrict login to one.
4190 * If the port type is physical clear the restrict login flag and return.
4191 * Else set the restrict login flag to val.
4192 *
4193 * Returns:
4194 * zero if val is in range
4195 * -EINVAL val out of range
4196 **/
4197static int
4198lpfc_restrict_login_init(struct lpfc_vport *vport, int val)
4199{
4200	if (val < 0 || val > 1) {
4201		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4202				 "0422 lpfc_restrict_login attribute cannot "
4203				 "be set to %d, allowed range is [0, 1]\n",
4204				 val);
4205		vport->cfg_restrict_login = 1;
4206		return -EINVAL;
4207	}
4208	if (vport->port_type == LPFC_PHYSICAL_PORT) {
4209		vport->cfg_restrict_login = 0;
4210		return 0;
4211	}
4212	vport->cfg_restrict_login = val;
4213	return 0;
4214}
4215
4216/**
4217 * lpfc_restrict_login_set - Set the vport restrict login flag
4218 * @vport: lpfc vport structure pointer.
4219 * @val: contains the restrict login value.
4220 *
4221 * Description:
4222 * If val is not in a valid range then log a kernel error message and set
4223 * the vport restrict login to one.
4224 * If the port type is physical and the val is not zero log a kernel
4225 * error message, clear the restrict login flag and return zero.
4226 * Else set the restrict login flag to val.
4227 *
4228 * Returns:
4229 * zero if val is in range
4230 * -EINVAL val out of range
4231 **/
4232static int
4233lpfc_restrict_login_set(struct lpfc_vport *vport, int val)
4234{
4235	if (val < 0 || val > 1) {
4236		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4237				 "0425 lpfc_restrict_login attribute cannot "
4238				 "be set to %d, allowed range is [0, 1]\n",
4239				 val);
4240		vport->cfg_restrict_login = 1;
4241		return -EINVAL;
4242	}
4243	if (vport->port_type == LPFC_PHYSICAL_PORT && val != 0) {
4244		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4245				 "0468 lpfc_restrict_login must be 0 for "
4246				 "Physical ports.\n");
4247		vport->cfg_restrict_login = 0;
4248		return 0;
4249	}
4250	vport->cfg_restrict_login = val;
4251	return 0;
4252}
4253lpfc_vport_param_store(restrict_login);
4254static DEVICE_ATTR_RW(lpfc_restrict_login);
4255
4256/*
4257# Some disk devices have a "select ID" or "select Target" capability.
4258# From a protocol standpoint "select ID" usually means select the
4259# Fibre channel "ALPA".  In the FC-AL Profile there is an "informative
4260# annex" which contains a table that maps a "select ID" (a number
4261# between 0 and 7F) to an ALPA.  By default, for compatibility with
4262# older drivers, the lpfc driver scans this table from low ALPA to high
4263# ALPA.
4264#
4265# Turning on the scan-down variable (on  = 1, off = 0) will
4266# cause the lpfc driver to use an inverted table, effectively
4267# scanning ALPAs from high to low. Value range is [0,1]. Default value is 1.
4268#
4269# (Note: This "select ID" functionality is a LOOP ONLY characteristic
4270# and will not work across a fabric. Also this parameter will take
4271# effect only in the case when ALPA map is not available.)
4272*/
4273LPFC_VPORT_ATTR_R(scan_down, 1, 0, 1,
4274		  "Start scanning for devices from highest ALPA to lowest");
4275
4276/*
4277# lpfc_topology:  link topology for init link
4278#            0x0  = attempt loop mode then point-to-point
4279#            0x01 = internal loopback mode
4280#            0x02 = attempt point-to-point mode only
4281#            0x04 = attempt loop mode only
4282#            0x06 = attempt point-to-point mode then loop
4283# Set point-to-point mode if you want to run as an N_Port.
4284# Set loop mode if you want to run as an NL_Port. Value range is [0,0x6].
4285# Default value is 0.
4286*/
4287LPFC_ATTR(topology, 0, 0, 6,
4288	"Select Fibre Channel topology");
4289
4290/**
4291 * lpfc_topology_store - Set the adapters topology field
4292 * @dev: class device that is converted into a scsi_host.
4293 * @attr:device attribute, not used.
4294 * @buf: buffer for passing information.
4295 * @count: size of the data buffer.
4296 *
4297 * Description:
4298 * If val is in a valid range then set the adapter's topology field and
4299 * issue a lip; if the lip fails reset the topology to the old value.
4300 *
4301 * If the value is not in range log a kernel error message and return an error.
4302 *
4303 * Returns:
4304 * zero if val is in range and lip okay
4305 * non-zero return value from lpfc_issue_lip()
4306 * -EINVAL val out of range
4307 **/
4308static ssize_t
4309lpfc_topology_store(struct device *dev, struct device_attribute *attr,
4310			const char *buf, size_t count)
4311{
4312	struct Scsi_Host  *shost = class_to_shost(dev);
4313	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4314	struct lpfc_hba   *phba = vport->phba;
4315	int val = 0;
4316	int nolip = 0;
4317	const char *val_buf = buf;
4318	int err;
4319	uint32_t prev_val;
4320	u8 sli_family, if_type;
4321
4322	if (!strncmp(buf, "nolip ", strlen("nolip "))) {
4323		nolip = 1;
4324		val_buf = &buf[strlen("nolip ")];
4325	}
4326
4327	if (!isdigit(val_buf[0]))
4328		return -EINVAL;
4329	if (sscanf(val_buf, "%i", &val) != 1)
4330		return -EINVAL;
4331
4332	if (val >= 0 && val <= 6) {
4333		prev_val = phba->cfg_topology;
4334		if (phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G &&
4335			val == 4) {
4336			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4337				"3113 Loop mode not supported at speed %d\n",
4338				val);
4339			return -EINVAL;
4340		}
4341		/*
4342		 * The 'topology' is not a configurable parameter if :
4343		 *   - persistent topology enabled
4344		 *   - ASIC_GEN_NUM >= 0xC, with no private loop support
4345		 */
4346		sli_family = bf_get(lpfc_sli_intf_sli_family,
4347				    &phba->sli4_hba.sli_intf);
4348		if_type = bf_get(lpfc_sli_intf_if_type,
4349				 &phba->sli4_hba.sli_intf);
4350		if ((test_bit(HBA_PERSISTENT_TOPO, &phba->hba_flag) ||
4351		     (!phba->sli4_hba.pc_sli4_params.pls &&
4352		     (sli_family == LPFC_SLI_INTF_FAMILY_G6 ||
4353		      if_type == LPFC_SLI_INTF_IF_TYPE_6))) &&
4354		    val == 4) {
4355			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4356				"3114 Loop mode not supported\n");
4357			return -EINVAL;
4358		}
4359		phba->cfg_topology = val;
4360		if (nolip)
4361			return strlen(buf);
4362
4363		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4364			"3054 lpfc_topology changed from %d to %d\n",
4365			prev_val, val);
4366		if (prev_val != val && phba->sli_rev == LPFC_SLI_REV4)
4367			phba->fc_topology_changed = 1;
4368		err = lpfc_issue_lip(lpfc_shost_from_vport(phba->pport));
4369		if (err) {
4370			phba->cfg_topology = prev_val;
4371			return -EINVAL;
4372		} else
4373			return strlen(buf);
4374	}
4375	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4376		"%d:0467 lpfc_topology attribute cannot be set to %d, "
4377		"allowed range is [0, 6]\n",
4378		phba->brd_no, val);
4379	return -EINVAL;
4380}
4381
4382lpfc_param_show(topology)
4383static DEVICE_ATTR_RW(lpfc_topology);
4384
4385/**
4386 * lpfc_static_vport_show: Read callback function for
4387 *   lpfc_static_vport sysfs file.
4388 * @dev: Pointer to class device object.
4389 * @attr: device attribute structure.
4390 * @buf: Data buffer.
4391 *
4392 * This function is the read call back function for
4393 * lpfc_static_vport sysfs file. The lpfc_static_vport
4394 * sysfs file report the mageability of the vport.
4395 **/
4396static ssize_t
4397lpfc_static_vport_show(struct device *dev, struct device_attribute *attr,
4398			 char *buf)
4399{
4400	struct Scsi_Host  *shost = class_to_shost(dev);
4401	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4402	if (vport->vport_flag & STATIC_VPORT)
4403		sprintf(buf, "1\n");
4404	else
4405		sprintf(buf, "0\n");
4406
4407	return strlen(buf);
4408}
4409
4410/*
4411 * Sysfs attribute to control the statistical data collection.
4412 */
4413static DEVICE_ATTR_RO(lpfc_static_vport);
4414
4415/*
4416# lpfc_link_speed: Link speed selection for initializing the Fibre Channel
4417# connection.
4418# Value range is [0,16]. Default value is 0.
4419*/
4420/**
4421 * lpfc_link_speed_store - Set the adapters link speed
4422 * @dev: Pointer to class device.
4423 * @attr: Unused.
4424 * @buf: Data buffer.
4425 * @count: Size of the data buffer.
4426 *
4427 * Description:
4428 * If val is in a valid range then set the adapter's link speed field and
4429 * issue a lip; if the lip fails reset the link speed to the old value.
4430 *
4431 * Notes:
4432 * If the value is not in range log a kernel error message and return an error.
4433 *
4434 * Returns:
4435 * zero if val is in range and lip okay.
4436 * non-zero return value from lpfc_issue_lip()
4437 * -EINVAL val out of range
4438 **/
4439static ssize_t
4440lpfc_link_speed_store(struct device *dev, struct device_attribute *attr,
4441		const char *buf, size_t count)
4442{
4443	struct Scsi_Host  *shost = class_to_shost(dev);
4444	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4445	struct lpfc_hba   *phba = vport->phba;
4446	int val = LPFC_USER_LINK_SPEED_AUTO;
4447	int nolip = 0;
4448	const char *val_buf = buf;
4449	int err;
4450	uint32_t prev_val, if_type;
4451
4452	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
4453	if (if_type >= LPFC_SLI_INTF_IF_TYPE_2 &&
4454	    test_bit(HBA_FORCED_LINK_SPEED, &phba->hba_flag))
4455		return -EPERM;
4456
4457	if (!strncmp(buf, "nolip ", strlen("nolip "))) {
4458		nolip = 1;
4459		val_buf = &buf[strlen("nolip ")];
4460	}
4461
4462	if (!isdigit(val_buf[0]))
4463		return -EINVAL;
4464	if (sscanf(val_buf, "%i", &val) != 1)
4465		return -EINVAL;
4466
4467	lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4468		"3055 lpfc_link_speed changed from %d to %d %s\n",
4469		phba->cfg_link_speed, val, nolip ? "(nolip)" : "(lip)");
4470
4471	if (((val == LPFC_USER_LINK_SPEED_1G) && !(phba->lmt & LMT_1Gb)) ||
4472	    ((val == LPFC_USER_LINK_SPEED_2G) && !(phba->lmt & LMT_2Gb)) ||
4473	    ((val == LPFC_USER_LINK_SPEED_4G) && !(phba->lmt & LMT_4Gb)) ||
4474	    ((val == LPFC_USER_LINK_SPEED_8G) && !(phba->lmt & LMT_8Gb)) ||
4475	    ((val == LPFC_USER_LINK_SPEED_10G) && !(phba->lmt & LMT_10Gb)) ||
4476	    ((val == LPFC_USER_LINK_SPEED_16G) && !(phba->lmt & LMT_16Gb)) ||
4477	    ((val == LPFC_USER_LINK_SPEED_32G) && !(phba->lmt & LMT_32Gb)) ||
4478	    ((val == LPFC_USER_LINK_SPEED_64G) && !(phba->lmt & LMT_64Gb))) {
4479		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4480				"2879 lpfc_link_speed attribute cannot be set "
4481				"to %d. Speed is not supported by this port.\n",
4482				val);
4483		return -EINVAL;
4484	}
4485	if (val >= LPFC_USER_LINK_SPEED_16G &&
4486	    phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
4487		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4488				"3112 lpfc_link_speed attribute cannot be set "
4489				"to %d. Speed is not supported in loop mode.\n",
4490				val);
4491		return -EINVAL;
4492	}
4493
4494	switch (val) {
4495	case LPFC_USER_LINK_SPEED_AUTO:
4496	case LPFC_USER_LINK_SPEED_1G:
4497	case LPFC_USER_LINK_SPEED_2G:
4498	case LPFC_USER_LINK_SPEED_4G:
4499	case LPFC_USER_LINK_SPEED_8G:
4500	case LPFC_USER_LINK_SPEED_16G:
4501	case LPFC_USER_LINK_SPEED_32G:
4502	case LPFC_USER_LINK_SPEED_64G:
4503		prev_val = phba->cfg_link_speed;
4504		phba->cfg_link_speed = val;
4505		if (nolip)
4506			return strlen(buf);
4507
4508		err = lpfc_issue_lip(lpfc_shost_from_vport(phba->pport));
4509		if (err) {
4510			phba->cfg_link_speed = prev_val;
4511			return -EINVAL;
4512		}
4513		return strlen(buf);
4514	default:
4515		break;
4516	}
4517
4518	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4519			"0469 lpfc_link_speed attribute cannot be set to %d, "
4520			"allowed values are [%s]\n",
4521			val, LPFC_LINK_SPEED_STRING);
4522	return -EINVAL;
4523
4524}
4525
4526static int lpfc_link_speed = 0;
4527module_param(lpfc_link_speed, int, S_IRUGO);
4528MODULE_PARM_DESC(lpfc_link_speed, "Select link speed");
4529lpfc_param_show(link_speed)
4530
4531/**
4532 * lpfc_link_speed_init - Set the adapters link speed
4533 * @phba: lpfc_hba pointer.
4534 * @val: link speed value.
4535 *
4536 * Description:
4537 * If val is in a valid range then set the adapter's link speed field.
4538 *
4539 * Notes:
4540 * If the value is not in range log a kernel error message, clear the link
4541 * speed and return an error.
4542 *
4543 * Returns:
4544 * zero if val saved.
4545 * -EINVAL val out of range
4546 **/
4547static int
4548lpfc_link_speed_init(struct lpfc_hba *phba, int val)
4549{
4550	if (val >= LPFC_USER_LINK_SPEED_16G && phba->cfg_topology == 4) {
4551		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4552			"3111 lpfc_link_speed of %d cannot "
4553			"support loop mode, setting topology to default.\n",
4554			 val);
4555		phba->cfg_topology = 0;
4556	}
4557
4558	switch (val) {
4559	case LPFC_USER_LINK_SPEED_AUTO:
4560	case LPFC_USER_LINK_SPEED_1G:
4561	case LPFC_USER_LINK_SPEED_2G:
4562	case LPFC_USER_LINK_SPEED_4G:
4563	case LPFC_USER_LINK_SPEED_8G:
4564	case LPFC_USER_LINK_SPEED_16G:
4565	case LPFC_USER_LINK_SPEED_32G:
4566	case LPFC_USER_LINK_SPEED_64G:
4567		phba->cfg_link_speed = val;
4568		return 0;
4569	default:
4570		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4571				"0405 lpfc_link_speed attribute cannot "
4572				"be set to %d, allowed values are "
4573				"["LPFC_LINK_SPEED_STRING"]\n", val);
4574		phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
4575		return -EINVAL;
4576	}
4577}
4578
4579static DEVICE_ATTR_RW(lpfc_link_speed);
4580
4581/*
4582# lpfc_aer_support: Support PCIe device Advanced Error Reporting (AER)
4583#       1  = aer supported and enabled (default)
4584# PCIe error reporting is always enabled by the PCI core, so this always
4585# shows 1.
4586#
4587# N.B. Parts of LPFC_ATTR open-coded since some of the underlying
4588# infrastructure (phba->cfg_aer_support) is gone.
4589*/
4590static uint lpfc_aer_support = 1;
4591module_param(lpfc_aer_support, uint, S_IRUGO);
4592MODULE_PARM_DESC(lpfc_aer_support, "Enable PCIe device AER support");
4593static ssize_t
4594lpfc_aer_support_show(struct device *dev, struct device_attribute *attr,
4595		      char *buf)
4596{
4597	return scnprintf(buf, PAGE_SIZE, "%d\n", lpfc_aer_support);
4598}
4599
4600/**
4601 * lpfc_aer_support_store - Set the adapter for aer support
4602 *
4603 * @dev: class device that is converted into a Scsi_host.
4604 * @attr: device attribute, not used.
4605 * @buf: containing enable or disable aer flag.
4606 * @count: unused variable.
4607 *
4608 * Description:
4609 * PCIe error reporting is enabled by the PCI core, so drivers don't need
4610 * to do anything.  Retain this interface for backwards compatibility,
4611 * but do nothing.
4612 *
4613 * Returns:
4614 * length of the buf on success
4615 * -EINVAL if val out of range
4616 **/
4617static ssize_t
4618lpfc_aer_support_store(struct device *dev, struct device_attribute *attr,
4619		       const char *buf, size_t count)
4620{
4621	int val = 0;
4622
4623	if (!isdigit(buf[0]))
4624		return -EINVAL;
4625	if (sscanf(buf, "%i", &val) != 1)
4626		return -EINVAL;
4627
4628	dev_info_once(dev, "PCIe error reporting automatically enabled by the PCI core; sysfs write ignored\n");
4629	return strlen(buf);
4630}
4631
4632static DEVICE_ATTR_RW(lpfc_aer_support);
4633
4634/**
4635 * lpfc_aer_cleanup_state - Clean up aer state to the aer enabled device
4636 * @dev: class device that is converted into a Scsi_host.
4637 * @attr: device attribute, not used.
4638 * @buf: containing flag 1 for aer cleanup state.
4639 * @count: unused variable.
4640 *
4641 * Description:
4642 * If the @buf contains 1, invokes the kernel AER helper routine
4643 * pci_aer_clear_nonfatal_status() to clean up the uncorrectable
4644 * error status register.
4645 *
4646 * Notes:
4647 *
4648 * Returns:
4649 * -EINVAL if the buf does not contain 1
4650 * -EPERM if the OS cannot clear AER error status, i.e., when platform
4651 * firmware owns the AER Capability
4652 **/
4653static ssize_t
4654lpfc_aer_cleanup_state(struct device *dev, struct device_attribute *attr,
4655		       const char *buf, size_t count)
4656{
4657	struct Scsi_Host  *shost = class_to_shost(dev);
4658	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4659	struct lpfc_hba   *phba = vport->phba;
4660	int val, rc = -1;
4661
4662	if (!isdigit(buf[0]))
4663		return -EINVAL;
4664	if (sscanf(buf, "%i", &val) != 1)
4665		return -EINVAL;
4666	if (val != 1)
4667		return -EINVAL;
4668
4669	rc = pci_aer_clear_nonfatal_status(phba->pcidev);
4670
4671	if (rc == 0)
4672		return strlen(buf);
4673	else
4674		return -EPERM;
4675}
4676
4677static DEVICE_ATTR(lpfc_aer_state_cleanup, S_IWUSR, NULL,
4678		   lpfc_aer_cleanup_state);
4679
4680/**
4681 * lpfc_sriov_nr_virtfn_store - Enable the adapter for sr-iov virtual functions
4682 *
4683 * @dev: class device that is converted into a Scsi_host.
4684 * @attr: device attribute, not used.
4685 * @buf: containing the string the number of vfs to be enabled.
4686 * @count: unused variable.
4687 *
4688 * Description:
4689 * When this api is called either through user sysfs, the driver shall
4690 * try to enable or disable SR-IOV virtual functions according to the
4691 * following:
4692 *
4693 * If zero virtual function has been enabled to the physical function,
4694 * the driver shall invoke the pci enable virtual function api trying
4695 * to enable the virtual functions. If the nr_vfn provided is greater
4696 * than the maximum supported, the maximum virtual function number will
4697 * be used for invoking the api; otherwise, the nr_vfn provided shall
4698 * be used for invoking the api. If the api call returned success, the
4699 * actual number of virtual functions enabled will be set to the driver
4700 * cfg_sriov_nr_virtfn; otherwise, -EINVAL shall be returned and driver
4701 * cfg_sriov_nr_virtfn remains zero.
4702 *
4703 * If none-zero virtual functions have already been enabled to the
4704 * physical function, as reflected by the driver's cfg_sriov_nr_virtfn,
4705 * -EINVAL will be returned and the driver does nothing;
4706 *
4707 * If the nr_vfn provided is zero and none-zero virtual functions have
4708 * been enabled, as indicated by the driver's cfg_sriov_nr_virtfn, the
4709 * disabling virtual function api shall be invoded to disable all the
4710 * virtual functions and driver's cfg_sriov_nr_virtfn shall be set to
4711 * zero. Otherwise, if zero virtual function has been enabled, do
4712 * nothing.
4713 *
4714 * Returns:
4715 * length of the buf on success if val is in range the intended mode
4716 * is supported.
4717 * -EINVAL if val out of range or intended mode is not supported.
4718 **/
4719static ssize_t
4720lpfc_sriov_nr_virtfn_store(struct device *dev, struct device_attribute *attr,
4721			 const char *buf, size_t count)
4722{
4723	struct Scsi_Host *shost = class_to_shost(dev);
4724	struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
4725	struct lpfc_hba *phba = vport->phba;
4726	struct pci_dev *pdev = phba->pcidev;
4727	int val = 0, rc = -EINVAL;
4728
4729	/* Sanity check on user data */
4730	if (!isdigit(buf[0]))
4731		return -EINVAL;
4732	if (sscanf(buf, "%i", &val) != 1)
4733		return -EINVAL;
4734	if (val < 0)
4735		return -EINVAL;
4736
4737	/* Request disabling virtual functions */
4738	if (val == 0) {
4739		if (phba->cfg_sriov_nr_virtfn > 0) {
4740			pci_disable_sriov(pdev);
4741			phba->cfg_sriov_nr_virtfn = 0;
4742		}
4743		return strlen(buf);
4744	}
4745
4746	/* Request enabling virtual functions */
4747	if (phba->cfg_sriov_nr_virtfn > 0) {
4748		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4749				"3018 There are %d virtual functions "
4750				"enabled on physical function.\n",
4751				phba->cfg_sriov_nr_virtfn);
4752		return -EEXIST;
4753	}
4754
4755	if (val <= LPFC_MAX_VFN_PER_PFN)
4756		phba->cfg_sriov_nr_virtfn = val;
4757	else {
4758		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4759				"3019 Enabling %d virtual functions is not "
4760				"allowed.\n", val);
4761		return -EINVAL;
4762	}
4763
4764	rc = lpfc_sli_probe_sriov_nr_virtfn(phba, phba->cfg_sriov_nr_virtfn);
4765	if (rc) {
4766		phba->cfg_sriov_nr_virtfn = 0;
4767		rc = -EPERM;
4768	} else
4769		rc = strlen(buf);
4770
4771	return rc;
4772}
4773
4774LPFC_ATTR(sriov_nr_virtfn, LPFC_DEF_VFN_PER_PFN, 0, LPFC_MAX_VFN_PER_PFN,
4775	"Enable PCIe device SR-IOV virtual fn");
4776
4777lpfc_param_show(sriov_nr_virtfn)
4778static DEVICE_ATTR_RW(lpfc_sriov_nr_virtfn);
4779
4780/**
4781 * lpfc_request_firmware_upgrade_store - Request for Linux generic firmware upgrade
4782 *
4783 * @dev: class device that is converted into a Scsi_host.
4784 * @attr: device attribute, not used.
4785 * @buf: containing the string the number of vfs to be enabled.
4786 * @count: unused variable.
4787 *
4788 * Description:
4789 *
4790 * Returns:
4791 * length of the buf on success if val is in range the intended mode
4792 * is supported.
4793 * -EINVAL if val out of range or intended mode is not supported.
4794 **/
4795static ssize_t
4796lpfc_request_firmware_upgrade_store(struct device *dev,
4797				    struct device_attribute *attr,
4798				    const char *buf, size_t count)
4799{
4800	struct Scsi_Host *shost = class_to_shost(dev);
4801	struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
4802	struct lpfc_hba *phba = vport->phba;
4803	int val = 0, rc;
4804
4805	/* Sanity check on user data */
4806	if (!isdigit(buf[0]))
4807		return -EINVAL;
4808	if (sscanf(buf, "%i", &val) != 1)
4809		return -EINVAL;
4810	if (val != 1)
4811		return -EINVAL;
4812
4813	rc = lpfc_sli4_request_firmware_update(phba, RUN_FW_UPGRADE);
4814	if (rc)
4815		rc = -EPERM;
4816	else
4817		rc = strlen(buf);
4818	return rc;
4819}
4820
4821static int lpfc_req_fw_upgrade;
4822module_param(lpfc_req_fw_upgrade, int, S_IRUGO|S_IWUSR);
4823MODULE_PARM_DESC(lpfc_req_fw_upgrade, "Enable Linux generic firmware upgrade");
4824lpfc_param_show(request_firmware_upgrade)
4825
4826/**
4827 * lpfc_request_firmware_upgrade_init - Enable initial linux generic fw upgrade
4828 * @phba: lpfc_hba pointer.
4829 * @val: 0 or 1.
4830 *
4831 * Description:
4832 * Set the initial Linux generic firmware upgrade enable or disable flag.
4833 *
4834 * Returns:
4835 * zero if val saved.
4836 * -EINVAL val out of range
4837 **/
4838static int
4839lpfc_request_firmware_upgrade_init(struct lpfc_hba *phba, int val)
4840{
4841	if (val >= 0 && val <= 1) {
4842		phba->cfg_request_firmware_upgrade = val;
4843		return 0;
4844	}
4845	return -EINVAL;
4846}
4847static DEVICE_ATTR(lpfc_req_fw_upgrade, S_IRUGO | S_IWUSR,
4848		   lpfc_request_firmware_upgrade_show,
4849		   lpfc_request_firmware_upgrade_store);
4850
4851/**
4852 * lpfc_force_rscn_store
4853 *
4854 * @dev: class device that is converted into a Scsi_host.
4855 * @attr: device attribute, not used.
4856 * @buf: unused string
4857 * @count: unused variable.
4858 *
4859 * Description:
4860 * Force the switch to send a RSCN to all other NPorts in our zone
4861 * If we are direct connect pt2pt, build the RSCN command ourself
4862 * and send to the other NPort. Not supported for private loop.
4863 *
4864 * Returns:
4865 * 0      - on success
4866 * -EIO   - if command is not sent
4867 **/
4868static ssize_t
4869lpfc_force_rscn_store(struct device *dev, struct device_attribute *attr,
4870		      const char *buf, size_t count)
4871{
4872	struct Scsi_Host *shost = class_to_shost(dev);
4873	struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
4874	int i;
4875
4876	i = lpfc_issue_els_rscn(vport, 0);
4877	if (i)
4878		return -EIO;
4879	return strlen(buf);
4880}
4881
4882/*
4883 * lpfc_force_rscn: Force an RSCN to be sent to all remote NPorts
4884 * connected to  the HBA.
4885 *
4886 * Value range is any ascii value
4887 */
4888static int lpfc_force_rscn;
4889module_param(lpfc_force_rscn, int, 0644);
4890MODULE_PARM_DESC(lpfc_force_rscn,
4891		 "Force an RSCN to be sent to all remote NPorts");
4892lpfc_param_show(force_rscn)
4893
4894/**
4895 * lpfc_force_rscn_init - Force an RSCN to be sent to all remote NPorts
4896 * @phba: lpfc_hba pointer.
4897 * @val: unused value.
4898 *
4899 * Returns:
4900 * zero if val saved.
4901 **/
4902static int
4903lpfc_force_rscn_init(struct lpfc_hba *phba, int val)
4904{
4905	return 0;
4906}
4907static DEVICE_ATTR_RW(lpfc_force_rscn);
4908
4909/**
4910 * lpfc_fcp_imax_store
4911 *
4912 * @dev: class device that is converted into a Scsi_host.
4913 * @attr: device attribute, not used.
4914 * @buf: string with the number of fast-path FCP interrupts per second.
4915 * @count: unused variable.
4916 *
4917 * Description:
4918 * If val is in a valid range [636,651042], then set the adapter's
4919 * maximum number of fast-path FCP interrupts per second.
4920 *
4921 * Returns:
4922 * length of the buf on success if val is in range the intended mode
4923 * is supported.
4924 * -EINVAL if val out of range or intended mode is not supported.
4925 **/
4926static ssize_t
4927lpfc_fcp_imax_store(struct device *dev, struct device_attribute *attr,
4928			 const char *buf, size_t count)
4929{
4930	struct Scsi_Host *shost = class_to_shost(dev);
4931	struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
4932	struct lpfc_hba *phba = vport->phba;
4933	struct lpfc_eq_intr_info *eqi;
4934	uint32_t usdelay;
4935	int val = 0, i;
4936
4937	/* fcp_imax is only valid for SLI4 */
4938	if (phba->sli_rev != LPFC_SLI_REV4)
4939		return -EINVAL;
4940
4941	/* Sanity check on user data */
4942	if (!isdigit(buf[0]))
4943		return -EINVAL;
4944	if (sscanf(buf, "%i", &val) != 1)
4945		return -EINVAL;
4946
4947	/*
4948	 * Value range for the HBA is [5000,5000000]
4949	 * The value for each EQ depends on how many EQs are configured.
4950	 * Allow value == 0
4951	 */
4952	if (val && (val < LPFC_MIN_IMAX || val > LPFC_MAX_IMAX))
4953		return -EINVAL;
4954
4955	phba->cfg_auto_imax = (val) ? 0 : 1;
4956	if (phba->cfg_fcp_imax && !val) {
4957		queue_delayed_work(phba->wq, &phba->eq_delay_work,
4958				   msecs_to_jiffies(LPFC_EQ_DELAY_MSECS));
4959
4960		for_each_present_cpu(i) {
4961			eqi = per_cpu_ptr(phba->sli4_hba.eq_info, i);
4962			eqi->icnt = 0;
4963		}
4964	}
4965
4966	phba->cfg_fcp_imax = (uint32_t)val;
4967
4968	if (phba->cfg_fcp_imax)
4969		usdelay = LPFC_SEC_TO_USEC / phba->cfg_fcp_imax;
4970	else
4971		usdelay = 0;
4972
4973	for (i = 0; i < phba->cfg_irq_chann; i += LPFC_MAX_EQ_DELAY_EQID_CNT)
4974		lpfc_modify_hba_eq_delay(phba, i, LPFC_MAX_EQ_DELAY_EQID_CNT,
4975					 usdelay);
4976
4977	return strlen(buf);
4978}
4979
4980/*
4981# lpfc_fcp_imax: The maximum number of fast-path FCP interrupts per second
4982# for the HBA.
4983#
4984# Value range is [5,000 to 5,000,000]. Default value is 50,000.
4985*/
4986static int lpfc_fcp_imax = LPFC_DEF_IMAX;
4987module_param(lpfc_fcp_imax, int, S_IRUGO|S_IWUSR);
4988MODULE_PARM_DESC(lpfc_fcp_imax,
4989	    "Set the maximum number of FCP interrupts per second per HBA");
4990lpfc_param_show(fcp_imax)
4991
4992/**
4993 * lpfc_fcp_imax_init - Set the initial sr-iov virtual function enable
4994 * @phba: lpfc_hba pointer.
4995 * @val: link speed value.
4996 *
4997 * Description:
4998 * If val is in a valid range [636,651042], then initialize the adapter's
4999 * maximum number of fast-path FCP interrupts per second.
5000 *
5001 * Returns:
5002 * zero if val saved.
5003 * -EINVAL val out of range
5004 **/
5005static int
5006lpfc_fcp_imax_init(struct lpfc_hba *phba, int val)
5007{
5008	if (phba->sli_rev != LPFC_SLI_REV4) {
5009		phba->cfg_fcp_imax = 0;
5010		return 0;
5011	}
5012
5013	if ((val >= LPFC_MIN_IMAX && val <= LPFC_MAX_IMAX) ||
5014	    (val == 0)) {
5015		phba->cfg_fcp_imax = val;
5016		return 0;
5017	}
5018
5019	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5020			"3016 lpfc_fcp_imax: %d out of range, using default\n",
5021			val);
5022	phba->cfg_fcp_imax = LPFC_DEF_IMAX;
5023
5024	return 0;
5025}
5026
5027static DEVICE_ATTR_RW(lpfc_fcp_imax);
5028
5029/**
5030 * lpfc_cq_max_proc_limit_store
5031 *
5032 * @dev: class device that is converted into a Scsi_host.
5033 * @attr: device attribute, not used.
5034 * @buf: string with the cq max processing limit of cqes
5035 * @count: unused variable.
5036 *
5037 * Description:
5038 * If val is in a valid range, then set value on each cq
5039 *
5040 * Returns:
5041 * The length of the buf: if successful
5042 * -ERANGE: if val is not in the valid range
5043 * -EINVAL: if bad value format or intended mode is not supported.
5044 **/
5045static ssize_t
5046lpfc_cq_max_proc_limit_store(struct device *dev, struct device_attribute *attr,
5047			 const char *buf, size_t count)
5048{
5049	struct Scsi_Host *shost = class_to_shost(dev);
5050	struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
5051	struct lpfc_hba *phba = vport->phba;
5052	struct lpfc_queue *eq, *cq;
5053	unsigned long val;
5054	int i;
5055
5056	/* cq_max_proc_limit is only valid for SLI4 */
5057	if (phba->sli_rev != LPFC_SLI_REV4)
5058		return -EINVAL;
5059
5060	/* Sanity check on user data */
5061	if (!isdigit(buf[0]))
5062		return -EINVAL;
5063	if (kstrtoul(buf, 0, &val))
5064		return -EINVAL;
5065
5066	if (val < LPFC_CQ_MIN_PROC_LIMIT || val > LPFC_CQ_MAX_PROC_LIMIT)
5067		return -ERANGE;
5068
5069	phba->cfg_cq_max_proc_limit = (uint32_t)val;
5070
5071	/* set the values on the cq's */
5072	for (i = 0; i < phba->cfg_irq_chann; i++) {
5073		/* Get the EQ corresponding to the IRQ vector */
5074		eq = phba->sli4_hba.hba_eq_hdl[i].eq;
5075		if (!eq)
5076			continue;
5077
5078		list_for_each_entry(cq, &eq->child_list, list)
5079			cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit,
5080						 cq->entry_count);
5081	}
5082
5083	return strlen(buf);
5084}
5085
5086/*
5087 * lpfc_cq_max_proc_limit: The maximum number CQE entries processed in an
5088 *   itteration of CQ processing.
5089 */
5090static int lpfc_cq_max_proc_limit = LPFC_CQ_DEF_MAX_PROC_LIMIT;
5091module_param(lpfc_cq_max_proc_limit, int, 0644);
5092MODULE_PARM_DESC(lpfc_cq_max_proc_limit,
5093	    "Set the maximum number CQEs processed in an iteration of "
5094	    "CQ processing");
5095lpfc_param_show(cq_max_proc_limit)
5096
5097/*
5098 * lpfc_cq_poll_threshold: Set the threshold of CQE completions in a
5099 *   single handler call which should request a polled completion rather
5100 *   than re-enabling interrupts.
5101 */
5102LPFC_ATTR_RW(cq_poll_threshold, LPFC_CQ_DEF_THRESHOLD_TO_POLL,
5103	     LPFC_CQ_MIN_THRESHOLD_TO_POLL,
5104	     LPFC_CQ_MAX_THRESHOLD_TO_POLL,
5105	     "CQE Processing Threshold to enable Polling");
5106
5107/**
5108 * lpfc_cq_max_proc_limit_init - Set the initial cq max_proc_limit
5109 * @phba: lpfc_hba pointer.
5110 * @val: entry limit
5111 *
5112 * Description:
5113 * If val is in a valid range, then initialize the adapter's maximum
5114 * value.
5115 *
5116 * Returns:
5117 *  Always returns 0 for success, even if value not always set to
5118 *  requested value. If value out of range or not supported, will fall
5119 *  back to default.
5120 **/
5121static int
5122lpfc_cq_max_proc_limit_init(struct lpfc_hba *phba, int val)
5123{
5124	phba->cfg_cq_max_proc_limit = LPFC_CQ_DEF_MAX_PROC_LIMIT;
5125
5126	if (phba->sli_rev != LPFC_SLI_REV4)
5127		return 0;
5128
5129	if (val >= LPFC_CQ_MIN_PROC_LIMIT && val <= LPFC_CQ_MAX_PROC_LIMIT) {
5130		phba->cfg_cq_max_proc_limit = val;
5131		return 0;
5132	}
5133
5134	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5135			"0371 lpfc_cq_max_proc_limit: %d out of range, using "
5136			"default\n",
5137			phba->cfg_cq_max_proc_limit);
5138
5139	return 0;
5140}
5141
5142static DEVICE_ATTR_RW(lpfc_cq_max_proc_limit);
5143
5144/**
5145 * lpfc_fcp_cpu_map_show - Display current driver CPU affinity
5146 * @dev: class converted to a Scsi_host structure.
5147 * @attr: device attribute, not used.
5148 * @buf: on return contains text describing the state of the link.
5149 *
5150 * Returns: size of formatted string.
5151 **/
5152static ssize_t
5153lpfc_fcp_cpu_map_show(struct device *dev, struct device_attribute *attr,
5154		      char *buf)
5155{
5156	struct Scsi_Host  *shost = class_to_shost(dev);
5157	struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
5158	struct lpfc_hba   *phba = vport->phba;
5159	struct lpfc_vector_map_info *cpup;
5160	int  len = 0;
5161
5162	if ((phba->sli_rev != LPFC_SLI_REV4) ||
5163	    (phba->intr_type != MSIX))
5164		return len;
5165
5166	switch (phba->cfg_fcp_cpu_map) {
5167	case 0:
5168		len += scnprintf(buf + len, PAGE_SIZE-len,
5169				"fcp_cpu_map: No mapping (%d)\n",
5170				phba->cfg_fcp_cpu_map);
5171		return len;
5172	case 1:
5173		len += scnprintf(buf + len, PAGE_SIZE-len,
5174				"fcp_cpu_map: HBA centric mapping (%d): "
5175				"%d of %d CPUs online from %d possible CPUs\n",
5176				phba->cfg_fcp_cpu_map, num_online_cpus(),
5177				num_present_cpus(),
5178				phba->sli4_hba.num_possible_cpu);
5179		break;
5180	}
5181
5182	while (phba->sli4_hba.curr_disp_cpu <
5183	       phba->sli4_hba.num_possible_cpu) {
5184		cpup = &phba->sli4_hba.cpu_map[phba->sli4_hba.curr_disp_cpu];
5185
5186		if (!cpu_present(phba->sli4_hba.curr_disp_cpu))
5187			len += scnprintf(buf + len, PAGE_SIZE - len,
5188					"CPU %02d not present\n",
5189					phba->sli4_hba.curr_disp_cpu);
5190		else if (cpup->eq == LPFC_VECTOR_MAP_EMPTY) {
5191			if (cpup->hdwq == LPFC_VECTOR_MAP_EMPTY)
5192				len += scnprintf(
5193					buf + len, PAGE_SIZE - len,
5194					"CPU %02d hdwq None "
5195					"physid %d coreid %d ht %d ua %d\n",
5196					phba->sli4_hba.curr_disp_cpu,
5197					cpup->phys_id, cpup->core_id,
5198					(cpup->flag & LPFC_CPU_MAP_HYPER),
5199					(cpup->flag & LPFC_CPU_MAP_UNASSIGN));
5200			else
5201				len += scnprintf(
5202					buf + len, PAGE_SIZE - len,
5203					"CPU %02d EQ None hdwq %04d "
5204					"physid %d coreid %d ht %d ua %d\n",
5205					phba->sli4_hba.curr_disp_cpu,
5206					cpup->hdwq, cpup->phys_id,
5207					cpup->core_id,
5208					(cpup->flag & LPFC_CPU_MAP_HYPER),
5209					(cpup->flag & LPFC_CPU_MAP_UNASSIGN));
5210		} else {
5211			if (cpup->hdwq == LPFC_VECTOR_MAP_EMPTY)
5212				len += scnprintf(
5213					buf + len, PAGE_SIZE - len,
5214					"CPU %02d hdwq None "
5215					"physid %d coreid %d ht %d ua %d IRQ %d\n",
5216					phba->sli4_hba.curr_disp_cpu,
5217					cpup->phys_id,
5218					cpup->core_id,
5219					(cpup->flag & LPFC_CPU_MAP_HYPER),
5220					(cpup->flag & LPFC_CPU_MAP_UNASSIGN),
5221					lpfc_get_irq(cpup->eq));
5222			else
5223				len += scnprintf(
5224					buf + len, PAGE_SIZE - len,
5225					"CPU %02d EQ %04d hdwq %04d "
5226					"physid %d coreid %d ht %d ua %d IRQ %d\n",
5227					phba->sli4_hba.curr_disp_cpu,
5228					cpup->eq, cpup->hdwq, cpup->phys_id,
5229					cpup->core_id,
5230					(cpup->flag & LPFC_CPU_MAP_HYPER),
5231					(cpup->flag & LPFC_CPU_MAP_UNASSIGN),
5232					lpfc_get_irq(cpup->eq));
5233		}
5234
5235		phba->sli4_hba.curr_disp_cpu++;
5236
5237		/* display max number of CPUs keeping some margin */
5238		if (phba->sli4_hba.curr_disp_cpu <
5239				phba->sli4_hba.num_possible_cpu &&
5240				(len >= (PAGE_SIZE - 64))) {
5241			len += scnprintf(buf + len,
5242					PAGE_SIZE - len, "more...\n");
5243			break;
5244		}
5245	}
5246
5247	if (phba->sli4_hba.curr_disp_cpu == phba->sli4_hba.num_possible_cpu)
5248		phba->sli4_hba.curr_disp_cpu = 0;
5249
5250	return len;
5251}
5252
5253/**
5254 * lpfc_fcp_cpu_map_store - Change CPU affinity of driver vectors
5255 * @dev: class device that is converted into a Scsi_host.
5256 * @attr: device attribute, not used.
5257 * @buf: one or more lpfc_polling_flags values.
5258 * @count: not used.
5259 *
5260 * Returns:
5261 * -EINVAL  - Not implemented yet.
5262 **/
5263static ssize_t
5264lpfc_fcp_cpu_map_store(struct device *dev, struct device_attribute *attr,
5265		       const char *buf, size_t count)
5266{
5267	return -EINVAL;
5268}
5269
5270/*
5271# lpfc_fcp_cpu_map: Defines how to map CPUs to IRQ vectors
5272# for the HBA.
5273#
5274# Value range is [0 to 1]. Default value is LPFC_HBA_CPU_MAP (1).
5275#	0 - Do not affinitze IRQ vectors
5276#	1 - Affintize HBA vectors with respect to each HBA
5277#	    (start with CPU0 for each HBA)
5278# This also defines how Hardware Queues are mapped to specific CPUs.
5279*/
5280static int lpfc_fcp_cpu_map = LPFC_HBA_CPU_MAP;
5281module_param(lpfc_fcp_cpu_map, int, S_IRUGO|S_IWUSR);
5282MODULE_PARM_DESC(lpfc_fcp_cpu_map,
5283		 "Defines how to map CPUs to IRQ vectors per HBA");
5284
5285/**
5286 * lpfc_fcp_cpu_map_init - Set the initial sr-iov virtual function enable
5287 * @phba: lpfc_hba pointer.
5288 * @val: link speed value.
5289 *
5290 * Description:
5291 * If val is in a valid range [0-2], then affinitze the adapter's
5292 * MSIX vectors.
5293 *
5294 * Returns:
5295 * zero if val saved.
5296 * -EINVAL val out of range
5297 **/
5298static int
5299lpfc_fcp_cpu_map_init(struct lpfc_hba *phba, int val)
5300{
5301	if (phba->sli_rev != LPFC_SLI_REV4) {
5302		phba->cfg_fcp_cpu_map = 0;
5303		return 0;
5304	}
5305
5306	if (val >= LPFC_MIN_CPU_MAP && val <= LPFC_MAX_CPU_MAP) {
5307		phba->cfg_fcp_cpu_map = val;
5308		return 0;
5309	}
5310
5311	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5312			"3326 lpfc_fcp_cpu_map: %d out of range, using "
5313			"default\n", val);
5314	phba->cfg_fcp_cpu_map = LPFC_HBA_CPU_MAP;
5315
5316	return 0;
5317}
5318
5319static DEVICE_ATTR_RW(lpfc_fcp_cpu_map);
5320
5321/*
5322# lpfc_fcp_class:  Determines FC class to use for the FCP protocol.
5323# Value range is [2,3]. Default value is 3.
5324*/
5325LPFC_VPORT_ATTR_R(fcp_class, 3, 2, 3,
5326		  "Select Fibre Channel class of service for FCP sequences");
5327
5328/*
5329# lpfc_use_adisc: Use ADISC for FCP rediscovery instead of PLOGI. Value range
5330# is [0,1]. Default value is 1.
5331*/
5332LPFC_VPORT_ATTR_RW(use_adisc, 1, 0, 1,
5333		   "Use ADISC on rediscovery to authenticate FCP devices");
5334
5335/*
5336# lpfc_first_burst_size: First burst size to use on the NPorts
5337# that support first burst.
5338# Value range is [0,65536]. Default value is 0.
5339*/
5340LPFC_VPORT_ATTR_RW(first_burst_size, 0, 0, 65536,
5341		   "First burst size for Targets that support first burst");
5342
5343/*
5344* lpfc_nvmet_fb_size: NVME Target mode supported first burst size.
5345* When the driver is configured as an NVME target, this value is
5346* communicated to the NVME initiator in the PRLI response.  It is
5347* used only when the lpfc_nvme_enable_fb and lpfc_nvmet_support
5348* parameters are set and the target is sending the PRLI RSP.
5349* Parameter supported on physical port only - no NPIV support.
5350* Value range is [0,65536]. Default value is 0.
5351*/
5352LPFC_ATTR_RW(nvmet_fb_size, 0, 0, 65536,
5353	     "NVME Target mode first burst size in 512B increments.");
5354
5355/*
5356 * lpfc_nvme_enable_fb: Enable NVME first burst on I and T functions.
5357 * For the Initiator (I), enabling this parameter means that an NVMET
5358 * PRLI response with FBA enabled and an FB_SIZE set to a nonzero value will be
5359 * processed by the initiator for subsequent NVME FCP IO.
5360 * Currently, this feature is not supported on the NVME target
5361 * Value range is [0,1]. Default value is 0 (disabled).
5362 */
5363LPFC_ATTR_RW(nvme_enable_fb, 0, 0, 1,
5364	     "Enable First Burst feature for NVME Initiator.");
5365
5366/*
5367# lpfc_max_scsicmpl_time: Use scsi command completion time to control I/O queue
5368# depth. Default value is 0. When the value of this parameter is zero the
5369# SCSI command completion time is not used for controlling I/O queue depth. When
5370# the parameter is set to a non-zero value, the I/O queue depth is controlled
5371# to limit the I/O completion time to the parameter value.
5372# The value is set in milliseconds.
5373*/
5374LPFC_VPORT_ATTR(max_scsicmpl_time, 0, 0, 60000,
5375	"Use command completion time to control queue depth");
5376
5377lpfc_vport_param_show(max_scsicmpl_time);
5378static int
5379lpfc_max_scsicmpl_time_set(struct lpfc_vport *vport, int val)
5380{
5381	struct lpfc_nodelist *ndlp, *next_ndlp;
5382	unsigned long iflags;
5383
5384	if (val == vport->cfg_max_scsicmpl_time)
5385		return 0;
5386	if ((val < 0) || (val > 60000))
5387		return -EINVAL;
5388	vport->cfg_max_scsicmpl_time = val;
5389
5390	spin_lock_irqsave(&vport->fc_nodes_list_lock, iflags);
5391	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
5392		if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
5393			continue;
5394		ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
5395	}
5396	spin_unlock_irqrestore(&vport->fc_nodes_list_lock, iflags);
5397	return 0;
5398}
5399lpfc_vport_param_store(max_scsicmpl_time);
5400static DEVICE_ATTR_RW(lpfc_max_scsicmpl_time);
5401
5402/*
5403# lpfc_ack0: Use ACK0, instead of ACK1 for class 2 acknowledgement. Value
5404# range is [0,1]. Default value is 0.
5405*/
5406LPFC_ATTR_R(ack0, 0, 0, 1, "Enable ACK0 support");
5407
5408/*
5409# lpfc_xri_rebalancing: enable or disable XRI rebalancing feature
5410# range is [0,1]. Default value is 1.
5411*/
5412LPFC_ATTR_R(xri_rebalancing, 1, 0, 1, "Enable/Disable XRI rebalancing");
5413
5414/*
5415 * lpfc_io_sched: Determine scheduling algrithmn for issuing FCP cmds
5416 * range is [0,1]. Default value is 0.
5417 * For [0], FCP commands are issued to Work Queues based on upper layer
5418 * hardware queue index.
5419 * For [1], FCP commands are issued to a Work Queue associated with the
5420 *          current CPU.
5421 *
5422 * LPFC_FCP_SCHED_BY_HDWQ == 0
5423 * LPFC_FCP_SCHED_BY_CPU == 1
5424 *
5425 * The driver dynamically sets this to 1 (BY_CPU) if it's able to set up cpu
5426 * affinity for FCP/NVME I/Os through Work Queues associated with the current
5427 * CPU. Otherwise, the default 0 (Round Robin) scheduling of FCP/NVME I/Os
5428 * through WQs will be used.
5429 */
5430LPFC_ATTR_RW(fcp_io_sched, LPFC_FCP_SCHED_BY_CPU,
5431	     LPFC_FCP_SCHED_BY_HDWQ,
5432	     LPFC_FCP_SCHED_BY_CPU,
5433	     "Determine scheduling algorithm for "
5434	     "issuing commands [0] - Hardware Queue, [1] - Current CPU");
5435
5436/*
5437 * lpfc_ns_query: Determine algrithmn for NameServer queries after RSCN
5438 * range is [0,1]. Default value is 0.
5439 * For [0], GID_FT is used for NameServer queries after RSCN (default)
5440 * For [1], GID_PT is used for NameServer queries after RSCN
5441 *
5442 */
5443LPFC_ATTR_RW(ns_query, LPFC_NS_QUERY_GID_FT,
5444	     LPFC_NS_QUERY_GID_FT, LPFC_NS_QUERY_GID_PT,
5445	     "Determine algorithm NameServer queries after RSCN "
5446	     "[0] - GID_FT, [1] - GID_PT");
5447
5448/*
5449# lpfc_fcp2_no_tgt_reset: Determine bus reset behavior
5450# range is [0,1]. Default value is 0.
5451# For [0], bus reset issues target reset to ALL devices
5452# For [1], bus reset issues target reset to non-FCP2 devices
5453*/
5454LPFC_ATTR_RW(fcp2_no_tgt_reset, 0, 0, 1, "Determine bus reset behavior for "
5455	     "FCP2 devices [0] - issue tgt reset, [1] - no tgt reset");
5456
5457
5458/*
5459# lpfc_cr_delay & lpfc_cr_count: Default values for I/O colaesing
5460# cr_delay (msec) or cr_count outstanding commands. cr_delay can take
5461# value [0,63]. cr_count can take value [1,255]. Default value of cr_delay
5462# is 0. Default value of cr_count is 1. The cr_count feature is disabled if
5463# cr_delay is set to 0.
5464*/
5465LPFC_ATTR_RW(cr_delay, 0, 0, 63, "A count of milliseconds after which an "
5466		"interrupt response is generated");
5467
5468LPFC_ATTR_RW(cr_count, 1, 1, 255, "A count of I/O completions after which an "
5469		"interrupt response is generated");
5470
5471/*
5472# lpfc_multi_ring_support:  Determines how many rings to spread available
5473# cmd/rsp IOCB entries across.
5474# Value range is [1,2]. Default value is 1.
5475*/
5476LPFC_ATTR_R(multi_ring_support, 1, 1, 2, "Determines number of primary "
5477		"SLI rings to spread IOCB entries across");
5478
5479/*
5480# lpfc_multi_ring_rctl:  If lpfc_multi_ring_support is enabled, this
5481# identifies what rctl value to configure the additional ring for.
5482# Value range is [1,0xff]. Default value is 4 (Unsolicated Data).
5483*/
5484LPFC_ATTR_R(multi_ring_rctl, FC_RCTL_DD_UNSOL_DATA, 1,
5485	     255, "Identifies RCTL for additional ring configuration");
5486
5487/*
5488# lpfc_multi_ring_type:  If lpfc_multi_ring_support is enabled, this
5489# identifies what type value to configure the additional ring for.
5490# Value range is [1,0xff]. Default value is 5 (LLC/SNAP).
5491*/
5492LPFC_ATTR_R(multi_ring_type, FC_TYPE_IP, 1,
5493	     255, "Identifies TYPE for additional ring configuration");
5494
5495/*
5496# lpfc_enable_SmartSAN: Sets up FDMI support for SmartSAN
5497#       0  = SmartSAN functionality disabled (default)
5498#       1  = SmartSAN functionality enabled
5499# This parameter will override the value of lpfc_fdmi_on module parameter.
5500# Value range is [0,1]. Default value is 0.
5501*/
5502LPFC_ATTR_R(enable_SmartSAN, 0, 0, 1, "Enable SmartSAN functionality");
5503
5504/*
5505# lpfc_fdmi_on: Controls FDMI support.
5506#       0       No FDMI support
5507#       1       Traditional FDMI support (default)
5508# Traditional FDMI support means the driver will assume FDMI-2 support;
5509# however, if that fails, it will fallback to FDMI-1.
5510# If lpfc_enable_SmartSAN is set to 1, the driver ignores lpfc_fdmi_on.
5511# If lpfc_enable_SmartSAN is set 0, the driver uses the current value of
5512# lpfc_fdmi_on.
5513# Value range [0,1]. Default value is 1.
5514*/
5515LPFC_ATTR_R(fdmi_on, 1, 0, 1, "Enable FDMI support");
5516
5517/*
5518# Specifies the maximum number of ELS cmds we can have outstanding (for
5519# discovery). Value range is [1,64]. Default value = 32.
5520*/
5521LPFC_VPORT_ATTR(discovery_threads, 32, 1, 64, "Maximum number of ELS commands "
5522		 "during discovery");
5523
5524/*
5525# lpfc_max_luns: maximum allowed LUN ID. This is the highest LUN ID that
5526#    will be scanned by the SCSI midlayer when sequential scanning is
5527#    used; and is also the highest LUN ID allowed when the SCSI midlayer
5528#    parses REPORT_LUN responses. The lpfc driver has no LUN count or
5529#    LUN ID limit, but the SCSI midlayer requires this field for the uses
5530#    above. The lpfc driver limits the default value to 255 for two reasons.
5531#    As it bounds the sequential scan loop, scanning for thousands of luns
5532#    on a target can take minutes of wall clock time.  Additionally,
5533#    there are FC targets, such as JBODs, that only recognize 8-bits of
5534#    LUN ID. When they receive a value greater than 8 bits, they chop off
5535#    the high order bits. In other words, they see LUN IDs 0, 256, 512,
5536#    and so on all as LUN ID 0. This causes the linux kernel, which sees
5537#    valid responses at each of the LUN IDs, to believe there are multiple
5538#    devices present, when in fact, there is only 1.
5539#    A customer that is aware of their target behaviors, and the results as
5540#    indicated above, is welcome to increase the lpfc_max_luns value.
5541#    As mentioned, this value is not used by the lpfc driver, only the
5542#    SCSI midlayer.
5543# Value range is [0,65535]. Default value is 255.
5544# NOTE: The SCSI layer might probe all allowed LUN on some old targets.
5545*/
5546LPFC_VPORT_ULL_ATTR_R(max_luns, 255, 0, 65535, "Maximum allowed LUN ID");
5547
5548/*
5549# lpfc_poll_tmo: .Milliseconds driver will wait between polling FCP ring.
5550# Value range is [1,255], default value is 10.
5551*/
5552LPFC_ATTR_RW(poll_tmo, 10, 1, 255,
5553	     "Milliseconds driver will wait between polling FCP ring");
5554
5555/*
5556# lpfc_task_mgmt_tmo: Maximum time to wait for task management commands
5557# to complete in seconds. Value range is [5,180], default value is 60.
5558*/
5559LPFC_ATTR_RW(task_mgmt_tmo, 60, 5, 180,
5560	     "Maximum time to wait for task management commands to complete");
5561/*
5562# lpfc_use_msi: Use MSI (Message Signaled Interrupts) in systems that
5563#		support this feature
5564#       0  = MSI disabled
5565#       1  = MSI enabled
5566#       2  = MSI-X enabled (default)
5567# Value range is [0,2]. Default value is 2.
5568*/
5569LPFC_ATTR_R(use_msi, 2, 0, 2, "Use Message Signaled Interrupts (1) or "
5570	    "MSI-X (2), if possible");
5571
5572/*
5573 * lpfc_nvme_oas: Use the oas bit when sending NVME/NVMET IOs
5574 *
5575 *      0  = NVME OAS disabled
5576 *      1  = NVME OAS enabled
5577 *
5578 * Value range is [0,1]. Default value is 0.
5579 */
5580LPFC_ATTR_RW(nvme_oas, 0, 0, 1,
5581	     "Use OAS bit on NVME IOs");
5582
5583/*
5584 * lpfc_nvme_embed_cmd: Use the oas bit when sending NVME/NVMET IOs
5585 *
5586 *      0  = Put NVME Command in SGL
5587 *      1  = Embed NVME Command in WQE (unless G7)
5588 *      2 =  Embed NVME Command in WQE (force)
5589 *
5590 * Value range is [0,2]. Default value is 1.
5591 */
5592LPFC_ATTR_RW(nvme_embed_cmd, 1, 0, 2,
5593	     "Embed NVME Command in WQE");
5594
5595/*
5596 * lpfc_fcp_mq_threshold: Set the maximum number of Hardware Queues
5597 * the driver will advertise it supports to the SCSI layer.
5598 *
5599 *      0    = Set nr_hw_queues by the number of CPUs or HW queues.
5600 *      1,256 = Manually specify nr_hw_queue value to be advertised,
5601 *
5602 * Value range is [0,256]. Default value is 8.
5603 */
5604LPFC_ATTR_R(fcp_mq_threshold, LPFC_FCP_MQ_THRESHOLD_DEF,
5605	    LPFC_FCP_MQ_THRESHOLD_MIN, LPFC_FCP_MQ_THRESHOLD_MAX,
5606	    "Set the number of SCSI Queues advertised");
5607
5608/*
5609 * lpfc_hdw_queue: Set the number of Hardware Queues the driver
5610 * will advertise it supports to the NVME and  SCSI layers. This also
5611 * will map to the number of CQ/WQ pairs the driver will create.
5612 *
5613 * The NVME Layer will try to create this many, plus 1 administrative
5614 * hardware queue. The administrative queue will always map to WQ 0
5615 * A hardware IO queue maps (qidx) to a specific driver CQ/WQ.
5616 *
5617 *      0    = Configure the number of hdw queues to the number of active CPUs.
5618 *      1,256 = Manually specify how many hdw queues to use.
5619 *
5620 * Value range is [0,256]. Default value is 0.
5621 */
5622LPFC_ATTR_R(hdw_queue,
5623	    LPFC_HBA_HDWQ_DEF,
5624	    LPFC_HBA_HDWQ_MIN, LPFC_HBA_HDWQ_MAX,
5625	    "Set the number of I/O Hardware Queues");
5626
5627#if IS_ENABLED(CONFIG_X86)
5628/**
5629 * lpfc_cpumask_irq_mode_init - initalizes cpumask of phba based on
5630 *				irq_chann_mode
5631 * @phba: Pointer to HBA context object.
5632 **/
5633static void
5634lpfc_cpumask_irq_mode_init(struct lpfc_hba *phba)
5635{
5636	unsigned int cpu, first_cpu, numa_node = NUMA_NO_NODE;
5637	const struct cpumask *sibling_mask;
5638	struct cpumask *aff_mask = &phba->sli4_hba.irq_aff_mask;
5639
5640	cpumask_clear(aff_mask);
5641
5642	if (phba->irq_chann_mode == NUMA_MODE) {
5643		/* Check if we're a NUMA architecture */
5644		numa_node = dev_to_node(&phba->pcidev->dev);
5645		if (numa_node == NUMA_NO_NODE) {
5646			phba->irq_chann_mode = NORMAL_MODE;
5647			return;
5648		}
5649	}
5650
5651	for_each_possible_cpu(cpu) {
5652		switch (phba->irq_chann_mode) {
5653		case NUMA_MODE:
5654			if (cpu_to_node(cpu) == numa_node)
5655				cpumask_set_cpu(cpu, aff_mask);
5656			break;
5657		case NHT_MODE:
5658			sibling_mask = topology_sibling_cpumask(cpu);
5659			first_cpu = cpumask_first(sibling_mask);
5660			if (first_cpu < nr_cpu_ids)
5661				cpumask_set_cpu(first_cpu, aff_mask);
5662			break;
5663		default:
5664			break;
5665		}
5666	}
5667}
5668#endif
5669
5670static void
5671lpfc_assign_default_irq_chann(struct lpfc_hba *phba)
5672{
5673#if IS_ENABLED(CONFIG_X86)
5674	switch (boot_cpu_data.x86_vendor) {
5675	case X86_VENDOR_AMD:
5676		/* If AMD architecture, then default is NUMA_MODE */
5677		phba->irq_chann_mode = NUMA_MODE;
5678		break;
5679	case X86_VENDOR_INTEL:
5680		/* If Intel architecture, then default is no hyperthread mode */
5681		phba->irq_chann_mode = NHT_MODE;
5682		break;
5683	default:
5684		phba->irq_chann_mode = NORMAL_MODE;
5685		break;
5686	}
5687	lpfc_cpumask_irq_mode_init(phba);
5688#else
5689	phba->irq_chann_mode = NORMAL_MODE;
5690#endif
5691}
5692
5693/*
5694 * lpfc_irq_chann: Set the number of IRQ vectors that are available
5695 * for Hardware Queues to utilize.  This also will map to the number
5696 * of EQ / MSI-X vectors the driver will create. This should never be
5697 * more than the number of Hardware Queues
5698 *
5699 *	0		= Configure number of IRQ Channels to:
5700 *			  if AMD architecture, number of CPUs on HBA's NUMA node
5701 *			  if Intel architecture, number of physical CPUs.
5702 *			  otherwise, number of active CPUs.
5703 *	[1,256]		= Manually specify how many IRQ Channels to use.
5704 *
5705 * Value range is [0,256]. Default value is [0].
5706 */
5707static uint lpfc_irq_chann = LPFC_IRQ_CHANN_DEF;
5708module_param(lpfc_irq_chann, uint, 0444);
5709MODULE_PARM_DESC(lpfc_irq_chann, "Set number of interrupt vectors to allocate");
5710
5711/* lpfc_irq_chann_init - Set the hba irq_chann initial value
5712 * @phba: lpfc_hba pointer.
5713 * @val: contains the initial value
5714 *
5715 * Description:
5716 * Validates the initial value is within range and assigns it to the
5717 * adapter. If not in range, an error message is posted and the
5718 * default value is assigned.
5719 *
5720 * Returns:
5721 * zero if value is in range and is set
5722 * -EINVAL if value was out of range
5723 **/
5724static int
5725lpfc_irq_chann_init(struct lpfc_hba *phba, uint32_t val)
5726{
5727	const struct cpumask *aff_mask;
5728
5729	if (phba->cfg_use_msi != 2) {
5730		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5731				"8532 use_msi = %u ignoring cfg_irq_numa\n",
5732				phba->cfg_use_msi);
5733		phba->irq_chann_mode = NORMAL_MODE;
5734		phba->cfg_irq_chann = LPFC_IRQ_CHANN_DEF;
5735		return 0;
5736	}
5737
5738	/* Check if default setting was passed */
5739	if (val == LPFC_IRQ_CHANN_DEF &&
5740	    phba->cfg_hdw_queue == LPFC_HBA_HDWQ_DEF &&
5741	    phba->sli_rev == LPFC_SLI_REV4)
5742		lpfc_assign_default_irq_chann(phba);
5743
5744	if (phba->irq_chann_mode != NORMAL_MODE) {
5745		aff_mask = &phba->sli4_hba.irq_aff_mask;
5746
5747		if (cpumask_empty(aff_mask)) {
5748			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5749					"8533 Could not identify CPUS for "
5750					"mode %d, ignoring\n",
5751					phba->irq_chann_mode);
5752			phba->irq_chann_mode = NORMAL_MODE;
5753			phba->cfg_irq_chann = LPFC_IRQ_CHANN_DEF;
5754		} else {
5755			phba->cfg_irq_chann = cpumask_weight(aff_mask);
5756
5757			/* If no hyperthread mode, then set hdwq count to
5758			 * aff_mask weight as well
5759			 */
5760			if (phba->irq_chann_mode == NHT_MODE)
5761				phba->cfg_hdw_queue = phba->cfg_irq_chann;
5762
5763			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5764					"8543 lpfc_irq_chann set to %u "
5765					"(mode: %d)\n", phba->cfg_irq_chann,
5766					phba->irq_chann_mode);
5767		}
5768	} else {
5769		if (val > LPFC_IRQ_CHANN_MAX) {
5770			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5771					"8545 lpfc_irq_chann attribute cannot "
5772					"be set to %u, allowed range is "
5773					"[%u,%u]\n",
5774					val,
5775					LPFC_IRQ_CHANN_MIN,
5776					LPFC_IRQ_CHANN_MAX);
5777			phba->cfg_irq_chann = LPFC_IRQ_CHANN_DEF;
5778			return -EINVAL;
5779		}
5780		if (phba->sli_rev == LPFC_SLI_REV4) {
5781			phba->cfg_irq_chann = val;
5782		} else {
5783			phba->cfg_irq_chann = 2;
5784			phba->cfg_hdw_queue = 1;
5785		}
5786	}
5787
5788	return 0;
5789}
5790
5791/**
5792 * lpfc_irq_chann_show - Display value of irq_chann
5793 * @dev: class converted to a Scsi_host structure.
5794 * @attr: device attribute, not used.
5795 * @buf: on return contains a string with the list sizes
5796 *
5797 * Returns: size of formatted string.
5798 **/
5799static ssize_t
5800lpfc_irq_chann_show(struct device *dev, struct device_attribute *attr,
5801		    char *buf)
5802{
5803	struct Scsi_Host *shost = class_to_shost(dev);
5804	struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
5805	struct lpfc_hba *phba = vport->phba;
5806
5807	return scnprintf(buf, PAGE_SIZE, "%u\n", phba->cfg_irq_chann);
5808}
5809
5810static DEVICE_ATTR_RO(lpfc_irq_chann);
5811
5812/*
5813# lpfc_enable_hba_reset: Allow or prevent HBA resets to the hardware.
5814#       0  = HBA resets disabled
5815#       1  = HBA resets enabled (default)
5816#       2  = HBA reset via PCI bus reset enabled
5817# Value range is [0,2]. Default value is 1.
5818*/
5819LPFC_ATTR_RW(enable_hba_reset, 1, 0, 2, "Enable HBA resets from the driver.");
5820
5821/*
5822# lpfc_enable_hba_heartbeat: Disable HBA heartbeat timer..
5823#       0  = HBA Heartbeat disabled
5824#       1  = HBA Heartbeat enabled (default)
5825# Value range is [0,1]. Default value is 1.
5826*/
5827LPFC_ATTR_R(enable_hba_heartbeat, 0, 0, 1, "Enable HBA Heartbeat.");
5828
5829/*
5830# lpfc_EnableXLane: Enable Express Lane Feature
5831#      0x0   Express Lane Feature disabled
5832#      0x1   Express Lane Feature enabled
5833# Value range is [0,1]. Default value is 0.
5834*/
5835LPFC_ATTR_R(EnableXLane, 0, 0, 1, "Enable Express Lane Feature.");
5836
5837/*
5838# lpfc_XLanePriority:  Define CS_CTL priority for Express Lane Feature
5839#       0x0 - 0x7f  = CS_CTL field in FC header (high 7 bits)
5840# Value range is [0x0,0x7f]. Default value is 0
5841*/
5842LPFC_ATTR_RW(XLanePriority, 0, 0x0, 0x7f, "CS_CTL for Express Lane Feature.");
5843
5844/*
5845# lpfc_enable_bg: Enable BlockGuard (Emulex's Implementation of T10-DIF)
5846#       0  = BlockGuard disabled (default)
5847#       1  = BlockGuard enabled
5848# Value range is [0,1]. Default value is 0.
5849*/
5850LPFC_ATTR_R(enable_bg, 0, 0, 1, "Enable BlockGuard Support");
5851
5852/*
5853# lpfc_prot_mask:
5854#	- Bit mask of host protection capabilities used to register with the
5855#	  SCSI mid-layer
5856# 	- Only meaningful if BG is turned on (lpfc_enable_bg=1).
5857#	- Allows you to ultimately specify which profiles to use
5858#	- Default will result in registering capabilities for all profiles.
5859#	- SHOST_DIF_TYPE1_PROTECTION	1
5860#		HBA supports T10 DIF Type 1: HBA to Target Type 1 Protection
5861#	- SHOST_DIX_TYPE0_PROTECTION	8
5862#		HBA supports DIX Type 0: Host to HBA protection only
5863#	- SHOST_DIX_TYPE1_PROTECTION	16
5864#		HBA supports DIX Type 1: Host to HBA  Type 1 protection
5865#
5866*/
5867LPFC_ATTR(prot_mask,
5868	(SHOST_DIF_TYPE1_PROTECTION |
5869	SHOST_DIX_TYPE0_PROTECTION |
5870	SHOST_DIX_TYPE1_PROTECTION),
5871	0,
5872	(SHOST_DIF_TYPE1_PROTECTION |
5873	SHOST_DIX_TYPE0_PROTECTION |
5874	SHOST_DIX_TYPE1_PROTECTION),
5875	"T10-DIF host protection capabilities mask");
5876
5877/*
5878# lpfc_prot_guard:
5879#	- Bit mask of protection guard types to register with the SCSI mid-layer
5880#	- Guard types are currently either 1) T10-DIF CRC 2) IP checksum
5881#	- Allows you to ultimately specify which profiles to use
5882#	- Default will result in registering capabilities for all guard types
5883#
5884*/
5885LPFC_ATTR(prot_guard,
5886	SHOST_DIX_GUARD_IP, SHOST_DIX_GUARD_CRC, SHOST_DIX_GUARD_IP,
5887	"T10-DIF host protection guard type");
5888
5889/*
5890 * Delay initial NPort discovery when Clean Address bit is cleared in
5891 * FLOGI/FDISC accept and FCID/Fabric name/Fabric portname is changed.
5892 * This parameter can have value 0 or 1.
5893 * When this parameter is set to 0, no delay is added to the initial
5894 * discovery.
5895 * When this parameter is set to non-zero value, initial Nport discovery is
5896 * delayed by ra_tov seconds when Clean Address bit is cleared in FLOGI/FDISC
5897 * accept and FCID/Fabric name/Fabric portname is changed.
5898 * Driver always delay Nport discovery for subsequent FLOGI/FDISC completion
5899 * when Clean Address bit is cleared in FLOGI/FDISC
5900 * accept and FCID/Fabric name/Fabric portname is changed.
5901 * Default value is 0.
5902 */
5903LPFC_ATTR(delay_discovery, 0, 0, 1,
5904	"Delay NPort discovery when Clean Address bit is cleared.");
5905
5906/*
5907 * lpfc_sg_seg_cnt - Initial Maximum DMA Segment Count
5908 * This value can be set to values between 64 and 4096. The default value
5909 * is 64, but may be increased to allow for larger Max I/O sizes. The scsi
5910 * and nvme layers will allow I/O sizes up to (MAX_SEG_COUNT * SEG_SIZE).
5911 * Because of the additional overhead involved in setting up T10-DIF,
5912 * this parameter will be limited to 128 if BlockGuard is enabled under SLI4
5913 * and will be limited to 512 if BlockGuard is enabled under SLI3.
5914 */
5915static uint lpfc_sg_seg_cnt = LPFC_DEFAULT_SG_SEG_CNT;
5916module_param(lpfc_sg_seg_cnt, uint, 0444);
5917MODULE_PARM_DESC(lpfc_sg_seg_cnt, "Max Scatter Gather Segment Count");
5918
5919/**
5920 * lpfc_sg_seg_cnt_show - Display the scatter/gather list sizes
5921 *    configured for the adapter
5922 * @dev: class converted to a Scsi_host structure.
5923 * @attr: device attribute, not used.
5924 * @buf: on return contains a string with the list sizes
5925 *
5926 * Returns: size of formatted string.
5927 **/
5928static ssize_t
5929lpfc_sg_seg_cnt_show(struct device *dev, struct device_attribute *attr,
5930		     char *buf)
5931{
5932	struct Scsi_Host  *shost = class_to_shost(dev);
5933	struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
5934	struct lpfc_hba   *phba = vport->phba;
5935	int len;
5936
5937	len = scnprintf(buf, PAGE_SIZE, "SGL sz: %d  total SGEs: %d\n",
5938		       phba->cfg_sg_dma_buf_size, phba->cfg_total_seg_cnt);
5939
5940	len += scnprintf(buf + len, PAGE_SIZE - len,
5941			"Cfg: %d  SCSI: %d  NVME: %d\n",
5942			phba->cfg_sg_seg_cnt, phba->cfg_scsi_seg_cnt,
5943			phba->cfg_nvme_seg_cnt);
5944	return len;
5945}
5946
5947static DEVICE_ATTR_RO(lpfc_sg_seg_cnt);
5948
5949/**
5950 * lpfc_sg_seg_cnt_init - Set the hba sg_seg_cnt initial value
5951 * @phba: lpfc_hba pointer.
5952 * @val: contains the initial value
5953 *
5954 * Description:
5955 * Validates the initial value is within range and assigns it to the
5956 * adapter. If not in range, an error message is posted and the
5957 * default value is assigned.
5958 *
5959 * Returns:
5960 * zero if value is in range and is set
5961 * -EINVAL if value was out of range
5962 **/
5963static int
5964lpfc_sg_seg_cnt_init(struct lpfc_hba *phba, int val)
5965{
5966	if (val >= LPFC_MIN_SG_SEG_CNT && val <= LPFC_MAX_SG_SEG_CNT) {
5967		phba->cfg_sg_seg_cnt = val;
5968		return 0;
5969	}
5970	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5971			"0409 lpfc_sg_seg_cnt attribute cannot be set to %d, "
5972			"allowed range is [%d, %d]\n",
5973			val, LPFC_MIN_SG_SEG_CNT, LPFC_MAX_SG_SEG_CNT);
5974	phba->cfg_sg_seg_cnt = LPFC_DEFAULT_SG_SEG_CNT;
5975	return -EINVAL;
5976}
5977
5978/*
5979 * lpfc_enable_mds_diags: Enable MDS Diagnostics
5980 *       0  = MDS Diagnostics disabled (default)
5981 *       1  = MDS Diagnostics enabled
5982 * Value range is [0,1]. Default value is 0.
5983 */
5984LPFC_ATTR_RW(enable_mds_diags, 0, 0, 1, "Enable MDS Diagnostics");
5985
5986/*
5987 * lpfc_ras_fwlog_buffsize: Firmware logging host buffer size
5988 *	0 = Disable firmware logging (default)
5989 *	[1-4] = Multiple of 1/4th Mb of host memory for FW logging
5990 * Value range [0..4]. Default value is 0
5991 */
5992LPFC_ATTR(ras_fwlog_buffsize, 0, 0, 4, "Host memory for FW logging");
5993lpfc_param_show(ras_fwlog_buffsize);
5994
5995static ssize_t
5996lpfc_ras_fwlog_buffsize_set(struct lpfc_hba  *phba, uint val)
5997{
5998	int ret = 0;
5999	enum ras_state state;
6000
6001	if (!lpfc_rangecheck(val, 0, 4))
6002		return -EINVAL;
6003
6004	if (phba->cfg_ras_fwlog_buffsize == val)
6005		return 0;
6006
6007	if (phba->cfg_ras_fwlog_func != PCI_FUNC(phba->pcidev->devfn))
6008		return -EINVAL;
6009
6010	spin_lock_irq(&phba->ras_fwlog_lock);
6011	state = phba->ras_fwlog.state;
6012	spin_unlock_irq(&phba->ras_fwlog_lock);
6013
6014	if (state == REG_INPROGRESS) {
6015		lpfc_printf_log(phba, KERN_ERR, LOG_SLI, "6147 RAS Logging "
6016				"registration is in progress\n");
6017		return -EBUSY;
6018	}
6019
6020	/* For disable logging: stop the logs and free the DMA.
6021	 * For ras_fwlog_buffsize size change we still need to free and
6022	 * reallocate the DMA in lpfc_sli4_ras_fwlog_init.
6023	 */
6024	phba->cfg_ras_fwlog_buffsize = val;
6025	if (state == ACTIVE) {
6026		lpfc_ras_stop_fwlog(phba);
6027		lpfc_sli4_ras_dma_free(phba);
6028	}
6029
6030	lpfc_sli4_ras_init(phba);
6031	if (phba->ras_fwlog.ras_enabled)
6032		ret = lpfc_sli4_ras_fwlog_init(phba, phba->cfg_ras_fwlog_level,
6033					       LPFC_RAS_ENABLE_LOGGING);
6034	return ret;
6035}
6036
6037lpfc_param_store(ras_fwlog_buffsize);
6038static DEVICE_ATTR_RW(lpfc_ras_fwlog_buffsize);
6039
6040/*
6041 * lpfc_ras_fwlog_level: Firmware logging verbosity level
6042 * Valid only if firmware logging is enabled
6043 * 0(Least Verbosity) 4 (most verbosity)
6044 * Value range is [0..4]. Default value is 0
6045 */
6046LPFC_ATTR_RW(ras_fwlog_level, 0, 0, 4, "Firmware Logging Level");
6047
6048/*
6049 * lpfc_ras_fwlog_func: Firmware logging enabled on function number
6050 * Default function which has RAS support : 0
6051 * Value Range is [0..3].
6052 * FW logging is a global action and enablement is via a specific
6053 * port.
6054 */
6055LPFC_ATTR_RW(ras_fwlog_func, 0, 0, 3, "Firmware Logging Enabled on Function");
6056
6057/*
6058 * lpfc_enable_bbcr: Enable BB Credit Recovery
6059 *       0  = BB Credit Recovery disabled
6060 *       1  = BB Credit Recovery enabled (default)
6061 * Value range is [0,1]. Default value is 1.
6062 */
6063LPFC_BBCR_ATTR_RW(enable_bbcr, 1, 0, 1, "Enable BBC Recovery");
6064
6065/* Signaling module parameters */
6066int lpfc_fabric_cgn_frequency = 100; /* 100 ms default */
6067module_param(lpfc_fabric_cgn_frequency, int, 0444);
6068MODULE_PARM_DESC(lpfc_fabric_cgn_frequency, "Congestion signaling fabric freq");
6069
6070unsigned char lpfc_acqe_cgn_frequency = 10; /* 10 sec default */
6071module_param(lpfc_acqe_cgn_frequency, byte, 0444);
6072MODULE_PARM_DESC(lpfc_acqe_cgn_frequency, "Congestion signaling ACQE freq");
6073
6074int lpfc_use_cgn_signal = 1; /* 0 - only use FPINs, 1 - Use signals if avail  */
6075module_param(lpfc_use_cgn_signal, int, 0444);
6076MODULE_PARM_DESC(lpfc_use_cgn_signal, "Use Congestion signaling if available");
6077
6078/*
6079 * lpfc_enable_dpp: Enable DPP on G7
6080 *       0  = DPP on G7 disabled
6081 *       1  = DPP on G7 enabled (default)
6082 * Value range is [0,1]. Default value is 1.
6083 */
6084LPFC_ATTR_RW(enable_dpp, 1, 0, 1, "Enable Direct Packet Push");
6085
6086/*
6087 * lpfc_enable_mi: Enable FDMI MIB
6088 *       0  = disabled
6089 *       1  = enabled (default)
6090 * Value range is [0,1].
6091 */
6092LPFC_ATTR_R(enable_mi, 1, 0, 1, "Enable MI");
6093
6094/*
6095 * lpfc_max_vmid: Maximum number of VMs to be tagged. This is valid only if
6096 * either vmid_app_header or vmid_priority_tagging is enabled.
6097 *       4 - 255  = vmid support enabled for 4-255 VMs
6098 *       Value range is [4,255].
6099 */
6100LPFC_ATTR_R(max_vmid, LPFC_MIN_VMID, LPFC_MIN_VMID, LPFC_MAX_VMID,
6101	     "Maximum number of VMs supported");
6102
6103/*
6104 * lpfc_vmid_inactivity_timeout: Inactivity timeout duration in hours
6105 *       0  = Timeout is disabled
6106 * Value range is [0,24].
6107 */
6108LPFC_ATTR_R(vmid_inactivity_timeout, 4, 0, 24,
6109	     "Inactivity timeout in hours");
6110
6111/*
6112 * lpfc_vmid_app_header: Enable App Header VMID support
6113 *       0  = Support is disabled (default)
6114 *       1  = Support is enabled
6115 * Value range is [0,1].
6116 */
6117LPFC_ATTR_R(vmid_app_header, LPFC_VMID_APP_HEADER_DISABLE,
6118	     LPFC_VMID_APP_HEADER_DISABLE, LPFC_VMID_APP_HEADER_ENABLE,
6119	     "Enable App Header VMID support");
6120
6121/*
6122 * lpfc_vmid_priority_tagging: Enable Priority Tagging VMID support
6123 *       0  = Support is disabled (default)
6124 *       1  = Allow supported targets only
6125 *       2  = Allow all targets
6126 * Value range is [0,2].
6127 */
6128LPFC_ATTR_R(vmid_priority_tagging, LPFC_VMID_PRIO_TAG_DISABLE,
6129	     LPFC_VMID_PRIO_TAG_DISABLE,
6130	     LPFC_VMID_PRIO_TAG_ALL_TARGETS,
6131	     "Enable Priority Tagging VMID support");
6132
6133static struct attribute *lpfc_hba_attrs[] = {
6134	&dev_attr_nvme_info.attr,
6135	&dev_attr_scsi_stat.attr,
6136	&dev_attr_bg_info.attr,
6137	&dev_attr_bg_guard_err.attr,
6138	&dev_attr_bg_apptag_err.attr,
6139	&dev_attr_bg_reftag_err.attr,
6140	&dev_attr_info.attr,
6141	&dev_attr_serialnum.attr,
6142	&dev_attr_modeldesc.attr,
6143	&dev_attr_modelname.attr,
6144	&dev_attr_programtype.attr,
6145	&dev_attr_portnum.attr,
6146	&dev_attr_fwrev.attr,
6147	&dev_attr_hdw.attr,
6148	&dev_attr_option_rom_version.attr,
6149	&dev_attr_link_state.attr,
6150	&dev_attr_num_discovered_ports.attr,
6151	&dev_attr_lpfc_drvr_version.attr,
6152	&dev_attr_lpfc_enable_fip.attr,
6153	&dev_attr_lpfc_temp_sensor.attr,
6154	&dev_attr_lpfc_log_verbose.attr,
6155	&dev_attr_lpfc_lun_queue_depth.attr,
6156	&dev_attr_lpfc_tgt_queue_depth.attr,
6157	&dev_attr_lpfc_hba_queue_depth.attr,
6158	&dev_attr_lpfc_peer_port_login.attr,
6159	&dev_attr_lpfc_nodev_tmo.attr,
6160	&dev_attr_lpfc_devloss_tmo.attr,
6161	&dev_attr_lpfc_enable_fc4_type.attr,
6162	&dev_attr_lpfc_fcp_class.attr,
6163	&dev_attr_lpfc_use_adisc.attr,
6164	&dev_attr_lpfc_first_burst_size.attr,
6165	&dev_attr_lpfc_ack0.attr,
6166	&dev_attr_lpfc_xri_rebalancing.attr,
6167	&dev_attr_lpfc_topology.attr,
6168	&dev_attr_lpfc_scan_down.attr,
6169	&dev_attr_lpfc_link_speed.attr,
6170	&dev_attr_lpfc_fcp_io_sched.attr,
6171	&dev_attr_lpfc_ns_query.attr,
6172	&dev_attr_lpfc_fcp2_no_tgt_reset.attr,
6173	&dev_attr_lpfc_cr_delay.attr,
6174	&dev_attr_lpfc_cr_count.attr,
6175	&dev_attr_lpfc_multi_ring_support.attr,
6176	&dev_attr_lpfc_multi_ring_rctl.attr,
6177	&dev_attr_lpfc_multi_ring_type.attr,
6178	&dev_attr_lpfc_fdmi_on.attr,
6179	&dev_attr_lpfc_enable_SmartSAN.attr,
6180	&dev_attr_lpfc_max_luns.attr,
6181	&dev_attr_lpfc_enable_npiv.attr,
6182	&dev_attr_lpfc_fcf_failover_policy.attr,
6183	&dev_attr_lpfc_enable_rrq.attr,
6184	&dev_attr_lpfc_fcp_wait_abts_rsp.attr,
6185	&dev_attr_nport_evt_cnt.attr,
6186	&dev_attr_board_mode.attr,
6187	&dev_attr_lpfc_xcvr_data.attr,
6188	&dev_attr_max_vpi.attr,
6189	&dev_attr_used_vpi.attr,
6190	&dev_attr_max_rpi.attr,
6191	&dev_attr_used_rpi.attr,
6192	&dev_attr_max_xri.attr,
6193	&dev_attr_used_xri.attr,
6194	&dev_attr_npiv_info.attr,
6195	&dev_attr_issue_reset.attr,
6196	&dev_attr_lpfc_poll.attr,
6197	&dev_attr_lpfc_poll_tmo.attr,
6198	&dev_attr_lpfc_task_mgmt_tmo.attr,
6199	&dev_attr_lpfc_use_msi.attr,
6200	&dev_attr_lpfc_nvme_oas.attr,
6201	&dev_attr_lpfc_nvme_embed_cmd.attr,
6202	&dev_attr_lpfc_fcp_imax.attr,
6203	&dev_attr_lpfc_force_rscn.attr,
6204	&dev_attr_lpfc_cq_poll_threshold.attr,
6205	&dev_attr_lpfc_cq_max_proc_limit.attr,
6206	&dev_attr_lpfc_fcp_cpu_map.attr,
6207	&dev_attr_lpfc_fcp_mq_threshold.attr,
6208	&dev_attr_lpfc_hdw_queue.attr,
6209	&dev_attr_lpfc_irq_chann.attr,
6210	&dev_attr_lpfc_suppress_rsp.attr,
6211	&dev_attr_lpfc_nvmet_mrq.attr,
6212	&dev_attr_lpfc_nvmet_mrq_post.attr,
6213	&dev_attr_lpfc_nvme_enable_fb.attr,
6214	&dev_attr_lpfc_nvmet_fb_size.attr,
6215	&dev_attr_lpfc_enable_bg.attr,
6216	&dev_attr_lpfc_enable_hba_reset.attr,
6217	&dev_attr_lpfc_enable_hba_heartbeat.attr,
6218	&dev_attr_lpfc_EnableXLane.attr,
6219	&dev_attr_lpfc_XLanePriority.attr,
6220	&dev_attr_lpfc_xlane_lun.attr,
6221	&dev_attr_lpfc_xlane_tgt.attr,
6222	&dev_attr_lpfc_xlane_vpt.attr,
6223	&dev_attr_lpfc_xlane_lun_state.attr,
6224	&dev_attr_lpfc_xlane_lun_status.attr,
6225	&dev_attr_lpfc_xlane_priority.attr,
6226	&dev_attr_lpfc_sg_seg_cnt.attr,
6227	&dev_attr_lpfc_max_scsicmpl_time.attr,
6228	&dev_attr_lpfc_aer_support.attr,
6229	&dev_attr_lpfc_aer_state_cleanup.attr,
6230	&dev_attr_lpfc_sriov_nr_virtfn.attr,
6231	&dev_attr_lpfc_req_fw_upgrade.attr,
6232	&dev_attr_lpfc_suppress_link_up.attr,
6233	&dev_attr_iocb_hw.attr,
6234	&dev_attr_pls.attr,
6235	&dev_attr_pt.attr,
6236	&dev_attr_txq_hw.attr,
6237	&dev_attr_txcmplq_hw.attr,
6238	&dev_attr_lpfc_sriov_hw_max_virtfn.attr,
6239	&dev_attr_protocol.attr,
6240	&dev_attr_lpfc_xlane_supported.attr,
6241	&dev_attr_lpfc_enable_mds_diags.attr,
6242	&dev_attr_lpfc_ras_fwlog_buffsize.attr,
6243	&dev_attr_lpfc_ras_fwlog_level.attr,
6244	&dev_attr_lpfc_ras_fwlog_func.attr,
6245	&dev_attr_lpfc_enable_bbcr.attr,
6246	&dev_attr_lpfc_enable_dpp.attr,
6247	&dev_attr_lpfc_enable_mi.attr,
6248	&dev_attr_cmf_info.attr,
6249	&dev_attr_lpfc_max_vmid.attr,
6250	&dev_attr_lpfc_vmid_inactivity_timeout.attr,
6251	&dev_attr_lpfc_vmid_app_header.attr,
6252	&dev_attr_lpfc_vmid_priority_tagging.attr,
6253	&dev_attr_lpfc_vmid_info.attr,
6254	NULL,
6255};
6256
6257static const struct attribute_group lpfc_hba_attr_group = {
6258	.attrs = lpfc_hba_attrs
6259};
6260
6261const struct attribute_group *lpfc_hba_groups[] = {
6262	&lpfc_hba_attr_group,
6263	NULL
6264};
6265
6266static struct attribute *lpfc_vport_attrs[] = {
6267	&dev_attr_info.attr,
6268	&dev_attr_link_state.attr,
6269	&dev_attr_num_discovered_ports.attr,
6270	&dev_attr_lpfc_drvr_version.attr,
6271	&dev_attr_lpfc_log_verbose.attr,
6272	&dev_attr_lpfc_lun_queue_depth.attr,
6273	&dev_attr_lpfc_tgt_queue_depth.attr,
6274	&dev_attr_lpfc_nodev_tmo.attr,
6275	&dev_attr_lpfc_devloss_tmo.attr,
6276	&dev_attr_lpfc_hba_queue_depth.attr,
6277	&dev_attr_lpfc_peer_port_login.attr,
6278	&dev_attr_lpfc_restrict_login.attr,
6279	&dev_attr_lpfc_fcp_class.attr,
6280	&dev_attr_lpfc_use_adisc.attr,
6281	&dev_attr_lpfc_first_burst_size.attr,
6282	&dev_attr_lpfc_max_luns.attr,
6283	&dev_attr_nport_evt_cnt.attr,
6284	&dev_attr_npiv_info.attr,
6285	&dev_attr_lpfc_enable_da_id.attr,
6286	&dev_attr_lpfc_max_scsicmpl_time.attr,
6287	&dev_attr_lpfc_static_vport.attr,
6288	&dev_attr_cmf_info.attr,
6289	NULL,
6290};
6291
6292static const struct attribute_group lpfc_vport_attr_group = {
6293	.attrs = lpfc_vport_attrs
6294};
6295
6296const struct attribute_group *lpfc_vport_groups[] = {
6297	&lpfc_vport_attr_group,
6298	NULL
6299};
6300
6301/**
6302 * sysfs_ctlreg_write - Write method for writing to ctlreg
6303 * @filp: open sysfs file
6304 * @kobj: kernel kobject that contains the kernel class device.
6305 * @bin_attr: kernel attributes passed to us.
6306 * @buf: contains the data to be written to the adapter IOREG space.
6307 * @off: offset into buffer to beginning of data.
6308 * @count: bytes to transfer.
6309 *
6310 * Description:
6311 * Accessed via /sys/class/scsi_host/hostxxx/ctlreg.
6312 * Uses the adapter io control registers to send buf contents to the adapter.
6313 *
6314 * Returns:
6315 * -ERANGE off and count combo out of range
6316 * -EINVAL off, count or buff address invalid
6317 * -EPERM adapter is offline
6318 * value of count, buf contents written
6319 **/
6320static ssize_t
6321sysfs_ctlreg_write(struct file *filp, struct kobject *kobj,
6322		   const struct bin_attribute *bin_attr,
6323		   char *buf, loff_t off, size_t count)
6324{
6325	size_t buf_off;
6326	struct device *dev = container_of(kobj, struct device, kobj);
6327	struct Scsi_Host  *shost = class_to_shost(dev);
6328	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
6329	struct lpfc_hba   *phba = vport->phba;
6330
6331	if (phba->sli_rev >= LPFC_SLI_REV4)
6332		return -EPERM;
6333
6334	if ((off + count) > FF_REG_AREA_SIZE)
6335		return -ERANGE;
6336
6337	if (count <= LPFC_REG_WRITE_KEY_SIZE)
6338		return 0;
6339
6340	if (off % 4 || count % 4 || (unsigned long)buf % 4)
6341		return -EINVAL;
6342
6343	/* This is to protect HBA registers from accidental writes. */
6344	if (memcmp(buf, LPFC_REG_WRITE_KEY, LPFC_REG_WRITE_KEY_SIZE))
6345		return -EINVAL;
6346
6347	if (!test_bit(FC_OFFLINE_MODE, &vport->fc_flag))
6348		return -EPERM;
6349
6350	spin_lock_irq(&phba->hbalock);
6351	for (buf_off = 0; buf_off < count - LPFC_REG_WRITE_KEY_SIZE;
6352			buf_off += sizeof(uint32_t))
6353		writel(*((uint32_t *)(buf + buf_off + LPFC_REG_WRITE_KEY_SIZE)),
6354		       phba->ctrl_regs_memmap_p + off + buf_off);
6355
6356	spin_unlock_irq(&phba->hbalock);
6357
6358	return count;
6359}
6360
6361/**
6362 * sysfs_ctlreg_read - Read method for reading from ctlreg
6363 * @filp: open sysfs file
6364 * @kobj: kernel kobject that contains the kernel class device.
6365 * @bin_attr: kernel attributes passed to us.
6366 * @buf: if successful contains the data from the adapter IOREG space.
6367 * @off: offset into buffer to beginning of data.
6368 * @count: bytes to transfer.
6369 *
6370 * Description:
6371 * Accessed via /sys/class/scsi_host/hostxxx/ctlreg.
6372 * Uses the adapter io control registers to read data into buf.
6373 *
6374 * Returns:
6375 * -ERANGE off and count combo out of range
6376 * -EINVAL off, count or buff address invalid
6377 * value of count, buf contents read
6378 **/
6379static ssize_t
6380sysfs_ctlreg_read(struct file *filp, struct kobject *kobj,
6381		  const struct bin_attribute *bin_attr,
6382		  char *buf, loff_t off, size_t count)
6383{
6384	size_t buf_off;
6385	uint32_t * tmp_ptr;
6386	struct device *dev = container_of(kobj, struct device, kobj);
6387	struct Scsi_Host  *shost = class_to_shost(dev);
6388	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
6389	struct lpfc_hba   *phba = vport->phba;
6390
6391	if (phba->sli_rev >= LPFC_SLI_REV4)
6392		return -EPERM;
6393
6394	if (off > FF_REG_AREA_SIZE)
6395		return -ERANGE;
6396
6397	if ((off + count) > FF_REG_AREA_SIZE)
6398		count = FF_REG_AREA_SIZE - off;
6399
6400	if (count == 0) return 0;
6401
6402	if (off % 4 || count % 4 || (unsigned long)buf % 4)
6403		return -EINVAL;
6404
6405	spin_lock_irq(&phba->hbalock);
6406
6407	for (buf_off = 0; buf_off < count; buf_off += sizeof(uint32_t)) {
6408		tmp_ptr = (uint32_t *)(buf + buf_off);
6409		*tmp_ptr = readl(phba->ctrl_regs_memmap_p + off + buf_off);
6410	}
6411
6412	spin_unlock_irq(&phba->hbalock);
6413
6414	return count;
6415}
6416
6417static const struct bin_attribute sysfs_ctlreg_attr = {
6418	.attr = {
6419		.name = "ctlreg",
6420		.mode = S_IRUSR | S_IWUSR,
6421	},
6422	.size = 256,
6423	.read = sysfs_ctlreg_read,
6424	.write = sysfs_ctlreg_write,
6425};
6426
6427/**
6428 * sysfs_mbox_write - Write method for writing information via mbox
6429 * @filp: open sysfs file
6430 * @kobj: kernel kobject that contains the kernel class device.
6431 * @bin_attr: kernel attributes passed to us.
6432 * @buf: contains the data to be written to sysfs mbox.
6433 * @off: offset into buffer to beginning of data.
6434 * @count: bytes to transfer.
6435 *
6436 * Description:
6437 * Deprecated function. All mailbox access from user space is performed via the
6438 * bsg interface.
6439 *
6440 * Returns:
6441 * -EPERM operation not permitted
6442 **/
6443static ssize_t
6444sysfs_mbox_write(struct file *filp, struct kobject *kobj,
6445		 const struct bin_attribute *bin_attr,
6446		 char *buf, loff_t off, size_t count)
6447{
6448	return -EPERM;
6449}
6450
6451/**
6452 * sysfs_mbox_read - Read method for reading information via mbox
6453 * @filp: open sysfs file
6454 * @kobj: kernel kobject that contains the kernel class device.
6455 * @bin_attr: kernel attributes passed to us.
6456 * @buf: contains the data to be read from sysfs mbox.
6457 * @off: offset into buffer to beginning of data.
6458 * @count: bytes to transfer.
6459 *
6460 * Description:
6461 * Deprecated function. All mailbox access from user space is performed via the
6462 * bsg interface.
6463 *
6464 * Returns:
6465 * -EPERM operation not permitted
6466 **/
6467static ssize_t
6468sysfs_mbox_read(struct file *filp, struct kobject *kobj,
6469		const struct bin_attribute *bin_attr,
6470		char *buf, loff_t off, size_t count)
6471{
6472	return -EPERM;
6473}
6474
6475static const struct bin_attribute sysfs_mbox_attr = {
6476	.attr = {
6477		.name = "mbox",
6478		.mode = S_IRUSR | S_IWUSR,
6479	},
6480	.size = MAILBOX_SYSFS_MAX,
6481	.read = sysfs_mbox_read,
6482	.write = sysfs_mbox_write,
6483};
6484
6485/**
6486 * lpfc_alloc_sysfs_attr - Creates the ctlreg and mbox entries
6487 * @vport: address of lpfc vport structure.
6488 *
6489 * Return codes:
6490 * zero on success
6491 * error return code from sysfs_create_bin_file()
6492 **/
6493int
6494lpfc_alloc_sysfs_attr(struct lpfc_vport *vport)
6495{
6496	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6497	int error;
6498
6499	/* Virtual ports do not need ctrl_reg and mbox */
6500	if (vport->port_type == LPFC_NPIV_PORT)
6501		return 0;
6502
6503	error = sysfs_create_bin_file(&shost->shost_dev.kobj,
6504				      &sysfs_ctlreg_attr);
6505	if (error)
6506		goto out;
6507
6508	error = sysfs_create_bin_file(&shost->shost_dev.kobj,
6509				      &sysfs_mbox_attr);
6510	if (error)
6511		goto out_remove_ctlreg_attr;
6512
6513	return 0;
6514out_remove_ctlreg_attr:
6515	sysfs_remove_bin_file(&shost->shost_dev.kobj, &sysfs_ctlreg_attr);
6516out:
6517	return error;
6518}
6519
6520/**
6521 * lpfc_free_sysfs_attr - Removes the ctlreg and mbox entries
6522 * @vport: address of lpfc vport structure.
6523 **/
6524void
6525lpfc_free_sysfs_attr(struct lpfc_vport *vport)
6526{
6527	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6528
6529	/* Virtual ports do not need ctrl_reg and mbox */
6530	if (vport->port_type == LPFC_NPIV_PORT)
6531		return;
6532	sysfs_remove_bin_file(&shost->shost_dev.kobj, &sysfs_mbox_attr);
6533	sysfs_remove_bin_file(&shost->shost_dev.kobj, &sysfs_ctlreg_attr);
6534}
6535
6536/*
6537 * Dynamic FC Host Attributes Support
6538 */
6539
6540/**
6541 * lpfc_get_host_symbolic_name - Copy symbolic name into the scsi host
6542 * @shost: kernel scsi host pointer.
6543 **/
6544static void
6545lpfc_get_host_symbolic_name(struct Scsi_Host *shost)
6546{
6547	struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
6548
6549	lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
6550				      sizeof fc_host_symbolic_name(shost));
6551}
6552
6553/**
6554 * lpfc_get_host_port_id - Copy the vport DID into the scsi host port id
6555 * @shost: kernel scsi host pointer.
6556 **/
6557static void
6558lpfc_get_host_port_id(struct Scsi_Host *shost)
6559{
6560	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
6561
6562	/* note: fc_myDID already in cpu endianness */
6563	fc_host_port_id(shost) = vport->fc_myDID;
6564}
6565
6566/**
6567 * lpfc_get_host_port_type - Set the value of the scsi host port type
6568 * @shost: kernel scsi host pointer.
6569 **/
6570static void
6571lpfc_get_host_port_type(struct Scsi_Host *shost)
6572{
6573	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
6574	struct lpfc_hba   *phba = vport->phba;
6575
6576	if (vport->port_type == LPFC_NPIV_PORT) {
6577		fc_host_port_type(shost) = FC_PORTTYPE_NPIV;
6578	} else if (lpfc_is_link_up(phba)) {
6579		if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
6580			if (test_bit(FC_PUBLIC_LOOP, &vport->fc_flag))
6581				fc_host_port_type(shost) = FC_PORTTYPE_NLPORT;
6582			else
6583				fc_host_port_type(shost) = FC_PORTTYPE_LPORT;
6584		} else {
6585			if (test_bit(FC_FABRIC, &vport->fc_flag))
6586				fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
6587			else
6588				fc_host_port_type(shost) = FC_PORTTYPE_PTP;
6589		}
6590	} else
6591		fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
6592}
6593
6594/**
6595 * lpfc_get_host_port_state - Set the value of the scsi host port state
6596 * @shost: kernel scsi host pointer.
6597 **/
6598static void
6599lpfc_get_host_port_state(struct Scsi_Host *shost)
6600{
6601	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
6602	struct lpfc_hba   *phba = vport->phba;
6603
6604	if (test_bit(FC_OFFLINE_MODE, &vport->fc_flag))
6605		fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
6606	else {
6607		switch (phba->link_state) {
6608		case LPFC_LINK_UNKNOWN:
6609		case LPFC_LINK_DOWN:
6610			fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
6611			break;
6612		case LPFC_LINK_UP:
6613		case LPFC_CLEAR_LA:
6614		case LPFC_HBA_READY:
6615			/* Links up, reports port state accordingly */
6616			if (vport->port_state < LPFC_VPORT_READY)
6617				fc_host_port_state(shost) =
6618							FC_PORTSTATE_BYPASSED;
6619			else
6620				fc_host_port_state(shost) =
6621							FC_PORTSTATE_ONLINE;
6622			break;
6623		case LPFC_HBA_ERROR:
6624			fc_host_port_state(shost) = FC_PORTSTATE_ERROR;
6625			break;
6626		default:
6627			fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
6628			break;
6629		}
6630	}
6631}
6632
6633/**
6634 * lpfc_get_host_speed - Set the value of the scsi host speed
6635 * @shost: kernel scsi host pointer.
6636 **/
6637static void
6638lpfc_get_host_speed(struct Scsi_Host *shost)
6639{
6640	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
6641	struct lpfc_hba   *phba = vport->phba;
6642
6643	if ((lpfc_is_link_up(phba)) &&
6644	    !test_bit(HBA_FCOE_MODE, &phba->hba_flag)) {
6645		switch(phba->fc_linkspeed) {
6646		case LPFC_LINK_SPEED_1GHZ:
6647			fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
6648			break;
6649		case LPFC_LINK_SPEED_2GHZ:
6650			fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
6651			break;
6652		case LPFC_LINK_SPEED_4GHZ:
6653			fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
6654			break;
6655		case LPFC_LINK_SPEED_8GHZ:
6656			fc_host_speed(shost) = FC_PORTSPEED_8GBIT;
6657			break;
6658		case LPFC_LINK_SPEED_10GHZ:
6659			fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
6660			break;
6661		case LPFC_LINK_SPEED_16GHZ:
6662			fc_host_speed(shost) = FC_PORTSPEED_16GBIT;
6663			break;
6664		case LPFC_LINK_SPEED_32GHZ:
6665			fc_host_speed(shost) = FC_PORTSPEED_32GBIT;
6666			break;
6667		case LPFC_LINK_SPEED_64GHZ:
6668			fc_host_speed(shost) = FC_PORTSPEED_64GBIT;
6669			break;
6670		case LPFC_LINK_SPEED_128GHZ:
6671			fc_host_speed(shost) = FC_PORTSPEED_128GBIT;
6672			break;
6673		case LPFC_LINK_SPEED_256GHZ:
6674			fc_host_speed(shost) = FC_PORTSPEED_256GBIT;
6675			break;
6676		default:
6677			fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
6678			break;
6679		}
6680	} else if (lpfc_is_link_up(phba) &&
6681		   test_bit(HBA_FCOE_MODE, &phba->hba_flag)) {
6682		switch (phba->fc_linkspeed) {
6683		case LPFC_ASYNC_LINK_SPEED_1GBPS:
6684			fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
6685			break;
6686		case LPFC_ASYNC_LINK_SPEED_10GBPS:
6687			fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
6688			break;
6689		case LPFC_ASYNC_LINK_SPEED_20GBPS:
6690			fc_host_speed(shost) = FC_PORTSPEED_20GBIT;
6691			break;
6692		case LPFC_ASYNC_LINK_SPEED_25GBPS:
6693			fc_host_speed(shost) = FC_PORTSPEED_25GBIT;
6694			break;
6695		case LPFC_ASYNC_LINK_SPEED_40GBPS:
6696			fc_host_speed(shost) = FC_PORTSPEED_40GBIT;
6697			break;
6698		case LPFC_ASYNC_LINK_SPEED_100GBPS:
6699			fc_host_speed(shost) = FC_PORTSPEED_100GBIT;
6700			break;
6701		default:
6702			fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
6703			break;
6704		}
6705	} else
6706		fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
6707}
6708
6709/**
6710 * lpfc_get_host_fabric_name - Set the value of the scsi host fabric name
6711 * @shost: kernel scsi host pointer.
6712 **/
6713static void
6714lpfc_get_host_fabric_name (struct Scsi_Host *shost)
6715{
6716	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
6717	struct lpfc_hba   *phba = vport->phba;
6718	u64 node_name;
6719
6720	if (vport->port_state > LPFC_FLOGI &&
6721	    (test_bit(FC_FABRIC, &vport->fc_flag) ||
6722	     (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
6723	      test_bit(FC_PUBLIC_LOOP, &vport->fc_flag))))
6724		node_name = wwn_to_u64(phba->fc_fabparam.nodeName.u.wwn);
6725	else
6726		/* fabric is local port if there is no F/FL_Port */
6727		node_name = 0;
6728
6729	fc_host_fabric_name(shost) = node_name;
6730}
6731
6732/**
6733 * lpfc_get_stats - Return statistical information about the adapter
6734 * @shost: kernel scsi host pointer.
6735 *
6736 * Notes:
6737 * NULL on error for link down, no mbox pool, sli2 active,
6738 * management not allowed, memory allocation error, or mbox error.
6739 *
6740 * Returns:
6741 * NULL for error
6742 * address of the adapter host statistics
6743 **/
6744static struct fc_host_statistics *
6745lpfc_get_stats(struct Scsi_Host *shost)
6746{
6747	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
6748	struct lpfc_hba   *phba = vport->phba;
6749	struct lpfc_sli   *psli = &phba->sli;
6750	struct fc_host_statistics *hs = &phba->link_stats;
6751	struct lpfc_lnk_stat * lso = &psli->lnk_stat_offsets;
6752	LPFC_MBOXQ_t *pmboxq;
6753	MAILBOX_t *pmb;
6754	int rc = 0;
6755
6756	/*
6757	 * prevent udev from issuing mailbox commands until the port is
6758	 * configured.
6759	 */
6760	if (phba->link_state < LPFC_LINK_DOWN ||
6761	    !phba->mbox_mem_pool ||
6762	    (phba->sli.sli_flag & LPFC_SLI_ACTIVE) == 0)
6763		return NULL;
6764
6765	if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO)
6766		return NULL;
6767
6768	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6769	if (!pmboxq)
6770		return NULL;
6771	memset(pmboxq, 0, sizeof (LPFC_MBOXQ_t));
6772
6773	pmb = &pmboxq->u.mb;
6774	pmb->mbxCommand = MBX_READ_STATUS;
6775	pmb->mbxOwner = OWN_HOST;
6776	pmboxq->ctx_buf = NULL;
6777	pmboxq->vport = vport;
6778
6779	if (test_bit(FC_OFFLINE_MODE, &vport->fc_flag)) {
6780		rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
6781		if (rc != MBX_SUCCESS) {
6782			mempool_free(pmboxq, phba->mbox_mem_pool);
6783			return NULL;
6784		}
6785	} else {
6786		rc = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
6787		if (rc != MBX_SUCCESS) {
6788			if (rc != MBX_TIMEOUT)
6789				mempool_free(pmboxq, phba->mbox_mem_pool);
6790			return NULL;
6791		}
6792	}
6793
6794	memset(hs, 0, sizeof (struct fc_host_statistics));
6795
6796	hs->tx_frames = pmb->un.varRdStatus.xmitFrameCnt;
6797	hs->rx_frames = pmb->un.varRdStatus.rcvFrameCnt;
6798
6799	/*
6800	 * The MBX_READ_STATUS returns tx_k_bytes which has to be
6801	 * converted to words.
6802	 *
6803	 * Check if extended byte flag is set, to know when to collect upper
6804	 * bits of 64 bit wide statistics counter.
6805	 */
6806	if (pmb->un.varRdStatus.xkb & RD_ST_XKB) {
6807		hs->tx_words = (u64)
6808			       ((((u64)(pmb->un.varRdStatus.xmit_xkb &
6809					RD_ST_XMIT_XKB_MASK) << 32) |
6810				(u64)pmb->un.varRdStatus.xmitByteCnt) *
6811				(u64)256);
6812		hs->rx_words = (u64)
6813			       ((((u64)(pmb->un.varRdStatus.rcv_xkb &
6814					RD_ST_RCV_XKB_MASK) << 32) |
6815				(u64)pmb->un.varRdStatus.rcvByteCnt) *
6816				(u64)256);
6817	} else {
6818		hs->tx_words = (uint64_t)
6819				((uint64_t)pmb->un.varRdStatus.xmitByteCnt
6820				* (uint64_t)256);
6821		hs->rx_words = (uint64_t)
6822				((uint64_t)pmb->un.varRdStatus.rcvByteCnt
6823				 * (uint64_t)256);
6824	}
6825
6826	memset(pmboxq, 0, sizeof (LPFC_MBOXQ_t));
6827	pmb->mbxCommand = MBX_READ_LNK_STAT;
6828	pmb->mbxOwner = OWN_HOST;
6829	pmboxq->ctx_buf = NULL;
6830	pmboxq->vport = vport;
6831
6832	if (test_bit(FC_OFFLINE_MODE, &vport->fc_flag)) {
6833		rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
6834		if (rc != MBX_SUCCESS) {
6835			mempool_free(pmboxq, phba->mbox_mem_pool);
6836			return NULL;
6837		}
6838	} else {
6839		rc = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
6840		if (rc != MBX_SUCCESS) {
6841			if (rc != MBX_TIMEOUT)
6842				mempool_free(pmboxq, phba->mbox_mem_pool);
6843			return NULL;
6844		}
6845	}
6846
6847	hs->link_failure_count = pmb->un.varRdLnk.linkFailureCnt;
6848	hs->loss_of_sync_count = pmb->un.varRdLnk.lossSyncCnt;
6849	hs->loss_of_signal_count = pmb->un.varRdLnk.lossSignalCnt;
6850	hs->prim_seq_protocol_err_count = pmb->un.varRdLnk.primSeqErrCnt;
6851	hs->invalid_tx_word_count = pmb->un.varRdLnk.invalidXmitWord;
6852	hs->invalid_crc_count = pmb->un.varRdLnk.crcCnt;
6853	hs->error_frames = pmb->un.varRdLnk.crcCnt;
6854
6855	hs->cn_sig_warn = atomic64_read(&phba->cgn_acqe_stat.warn);
6856	hs->cn_sig_alarm = atomic64_read(&phba->cgn_acqe_stat.alarm);
6857
6858	hs->link_failure_count -= lso->link_failure_count;
6859	hs->loss_of_sync_count -= lso->loss_of_sync_count;
6860	hs->loss_of_signal_count -= lso->loss_of_signal_count;
6861	hs->prim_seq_protocol_err_count -= lso->prim_seq_protocol_err_count;
6862	hs->invalid_tx_word_count -= lso->invalid_tx_word_count;
6863	hs->invalid_crc_count -= lso->invalid_crc_count;
6864	hs->error_frames -= lso->error_frames;
6865
6866	if (test_bit(HBA_FCOE_MODE, &phba->hba_flag)) {
6867		hs->lip_count = -1;
6868		hs->nos_count = (phba->link_events >> 1);
6869		hs->nos_count -= lso->link_events;
6870	} else if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
6871		hs->lip_count = (phba->fc_eventTag >> 1);
6872		hs->lip_count -= lso->link_events;
6873		hs->nos_count = -1;
6874	} else {
6875		hs->lip_count = -1;
6876		hs->nos_count = (phba->fc_eventTag >> 1);
6877		hs->nos_count -= lso->link_events;
6878	}
6879
6880	hs->dumped_frames = -1;
6881
6882	hs->seconds_since_last_reset = ktime_get_seconds() - psli->stats_start;
6883
6884	mempool_free(pmboxq, phba->mbox_mem_pool);
6885
6886	return hs;
6887}
6888
6889/**
6890 * lpfc_reset_stats - Copy the adapter link stats information
6891 * @shost: kernel scsi host pointer.
6892 **/
6893static void
6894lpfc_reset_stats(struct Scsi_Host *shost)
6895{
6896	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
6897	struct lpfc_hba   *phba = vport->phba;
6898	struct lpfc_sli   *psli = &phba->sli;
6899	struct lpfc_lnk_stat *lso = &psli->lnk_stat_offsets;
6900	LPFC_MBOXQ_t *pmboxq;
6901	MAILBOX_t *pmb;
6902	int rc = 0;
6903
6904	if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO)
6905		return;
6906
6907	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6908	if (!pmboxq)
6909		return;
6910	memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
6911
6912	pmb = &pmboxq->u.mb;
6913	pmb->mbxCommand = MBX_READ_STATUS;
6914	pmb->mbxOwner = OWN_HOST;
6915	pmb->un.varWords[0] = 0x1; /* reset request */
6916	pmboxq->ctx_buf = NULL;
6917	pmboxq->vport = vport;
6918
6919	if (test_bit(FC_OFFLINE_MODE, &vport->fc_flag) ||
6920	    !(psli->sli_flag & LPFC_SLI_ACTIVE)) {
6921		rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
6922		if (rc != MBX_SUCCESS) {
6923			mempool_free(pmboxq, phba->mbox_mem_pool);
6924			return;
6925		}
6926	} else {
6927		rc = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
6928		if (rc != MBX_SUCCESS) {
6929			if (rc != MBX_TIMEOUT)
6930				mempool_free(pmboxq, phba->mbox_mem_pool);
6931			return;
6932		}
6933	}
6934
6935	memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
6936	pmb->mbxCommand = MBX_READ_LNK_STAT;
6937	pmb->mbxOwner = OWN_HOST;
6938	pmboxq->ctx_buf = NULL;
6939	pmboxq->vport = vport;
6940
6941	if (test_bit(FC_OFFLINE_MODE, &vport->fc_flag) ||
6942	    !(psli->sli_flag & LPFC_SLI_ACTIVE)) {
6943		rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
6944		if (rc != MBX_SUCCESS) {
6945			mempool_free(pmboxq, phba->mbox_mem_pool);
6946			return;
6947		}
6948	} else {
6949		rc = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
6950		if (rc != MBX_SUCCESS) {
6951			if (rc != MBX_TIMEOUT)
6952				mempool_free(pmboxq, phba->mbox_mem_pool);
6953			return;
6954		}
6955	}
6956
6957	lso->link_failure_count = pmb->un.varRdLnk.linkFailureCnt;
6958	lso->loss_of_sync_count = pmb->un.varRdLnk.lossSyncCnt;
6959	lso->loss_of_signal_count = pmb->un.varRdLnk.lossSignalCnt;
6960	lso->prim_seq_protocol_err_count = pmb->un.varRdLnk.primSeqErrCnt;
6961	lso->invalid_tx_word_count = pmb->un.varRdLnk.invalidXmitWord;
6962	lso->invalid_crc_count = pmb->un.varRdLnk.crcCnt;
6963	lso->error_frames = pmb->un.varRdLnk.crcCnt;
6964	if (test_bit(HBA_FCOE_MODE, &phba->hba_flag))
6965		lso->link_events = (phba->link_events >> 1);
6966	else
6967		lso->link_events = (phba->fc_eventTag >> 1);
6968
6969	atomic64_set(&phba->cgn_acqe_stat.warn, 0);
6970	atomic64_set(&phba->cgn_acqe_stat.alarm, 0);
6971
6972	memset(&shost_to_fc_host(shost)->fpin_stats, 0,
6973	       sizeof(shost_to_fc_host(shost)->fpin_stats));
6974
6975	psli->stats_start = ktime_get_seconds();
6976
6977	mempool_free(pmboxq, phba->mbox_mem_pool);
6978
6979	return;
6980}
6981
6982/*
6983 * The LPFC driver treats linkdown handling as target loss events so there
6984 * are no sysfs handlers for link_down_tmo.
6985 */
6986
6987/**
6988 * lpfc_get_node_by_target - Return the nodelist for a target
6989 * @starget: kernel scsi target pointer.
6990 *
6991 * Returns:
6992 * address of the node list if found
6993 * NULL target not found
6994 **/
6995static struct lpfc_nodelist *
6996lpfc_get_node_by_target(struct scsi_target *starget)
6997{
6998	struct Scsi_Host  *shost = dev_to_shost(starget->dev.parent);
6999	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
7000	struct lpfc_nodelist *ndlp;
7001	unsigned long iflags;
7002
7003	spin_lock_irqsave(&vport->fc_nodes_list_lock, iflags);
7004	/* Search for this, mapped, target ID */
7005	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
7006		if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
7007		    starget->id == ndlp->nlp_sid) {
7008			spin_unlock_irqrestore(&vport->fc_nodes_list_lock,
7009					       iflags);
7010			return ndlp;
7011		}
7012	}
7013	spin_unlock_irqrestore(&vport->fc_nodes_list_lock, iflags);
7014	return NULL;
7015}
7016
7017/**
7018 * lpfc_get_starget_port_id - Set the target port id to the ndlp DID or -1
7019 * @starget: kernel scsi target pointer.
7020 **/
7021static void
7022lpfc_get_starget_port_id(struct scsi_target *starget)
7023{
7024	struct lpfc_nodelist *ndlp = lpfc_get_node_by_target(starget);
7025
7026	fc_starget_port_id(starget) = ndlp ? ndlp->nlp_DID : -1;
7027}
7028
7029/**
7030 * lpfc_get_starget_node_name - Set the target node name
7031 * @starget: kernel scsi target pointer.
7032 *
7033 * Description: Set the target node name to the ndlp node name wwn or zero.
7034 **/
7035static void
7036lpfc_get_starget_node_name(struct scsi_target *starget)
7037{
7038	struct lpfc_nodelist *ndlp = lpfc_get_node_by_target(starget);
7039
7040	fc_starget_node_name(starget) =
7041		ndlp ? wwn_to_u64(ndlp->nlp_nodename.u.wwn) : 0;
7042}
7043
7044/**
7045 * lpfc_get_starget_port_name - Set the target port name
7046 * @starget: kernel scsi target pointer.
7047 *
7048 * Description:  set the target port name to the ndlp port name wwn or zero.
7049 **/
7050static void
7051lpfc_get_starget_port_name(struct scsi_target *starget)
7052{
7053	struct lpfc_nodelist *ndlp = lpfc_get_node_by_target(starget);
7054
7055	fc_starget_port_name(starget) =
7056		ndlp ? wwn_to_u64(ndlp->nlp_portname.u.wwn) : 0;
7057}
7058
7059/**
7060 * lpfc_set_rport_loss_tmo - Set the rport dev loss tmo
7061 * @rport: fc rport address.
7062 * @timeout: new value for dev loss tmo.
7063 *
7064 * Description:
7065 * If timeout is non zero set the dev_loss_tmo to timeout, else set
7066 * dev_loss_tmo to one.
7067 **/
7068static void
7069lpfc_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
7070{
7071	struct lpfc_rport_data *rdata = rport->dd_data;
7072	struct lpfc_nodelist *ndlp = rdata->pnode;
7073#if (IS_ENABLED(CONFIG_NVME_FC))
7074	struct lpfc_nvme_rport *nrport = NULL;
7075#endif
7076
7077	if (timeout)
7078		rport->dev_loss_tmo = timeout;
7079	else
7080		rport->dev_loss_tmo = 1;
7081
7082	if (!ndlp) {
7083		dev_info(&rport->dev, "Cannot find remote node to "
7084				      "set rport dev loss tmo, port_id x%x\n",
7085				      rport->port_id);
7086		return;
7087	}
7088
7089#if (IS_ENABLED(CONFIG_NVME_FC))
7090	nrport = lpfc_ndlp_get_nrport(ndlp);
7091
7092	if (nrport && nrport->remoteport)
7093		nvme_fc_set_remoteport_devloss(nrport->remoteport,
7094					       rport->dev_loss_tmo);
7095#endif
7096}
7097
7098/*
7099 * lpfc_rport_show_function - Return rport target information
7100 *
7101 * Description:
7102 * Macro that uses field to generate a function with the name lpfc_show_rport_
7103 *
7104 * lpfc_show_rport_##field: returns the bytes formatted in buf
7105 * @cdev: class converted to an fc_rport.
7106 * @buf: on return contains the target_field or zero.
7107 *
7108 * Returns: size of formatted string.
7109 **/
7110#define lpfc_rport_show_function(field, format_string, sz, cast)	\
7111static ssize_t								\
7112lpfc_show_rport_##field (struct device *dev,				\
7113			 struct device_attribute *attr,			\
7114			 char *buf)					\
7115{									\
7116	struct fc_rport *rport = transport_class_to_rport(dev);		\
7117	struct lpfc_rport_data *rdata = rport->hostdata;		\
7118	return scnprintf(buf, sz, format_string,			\
7119		(rdata->target) ? cast rdata->target->field : 0);	\
7120}
7121
7122#define lpfc_rport_rd_attr(field, format_string, sz)			\
7123	lpfc_rport_show_function(field, format_string, sz, )		\
7124static FC_RPORT_ATTR(field, S_IRUGO, lpfc_show_rport_##field, NULL)
7125
7126/**
7127 * lpfc_set_vport_symbolic_name - Set the vport's symbolic name
7128 * @fc_vport: The fc_vport who's symbolic name has been changed.
7129 *
7130 * Description:
7131 * This function is called by the transport after the @fc_vport's symbolic name
7132 * has been changed. This function re-registers the symbolic name with the
7133 * switch to propagate the change into the fabric if the vport is active.
7134 **/
7135static void
7136lpfc_set_vport_symbolic_name(struct fc_vport *fc_vport)
7137{
7138	struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
7139
7140	if (vport->port_state == LPFC_VPORT_READY)
7141		lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
7142}
7143
7144/**
7145 * lpfc_hba_log_verbose_init - Set hba's log verbose level
7146 * @phba: Pointer to lpfc_hba struct.
7147 * @verbose: Verbose level to set.
7148 *
7149 * This function is called by the lpfc_get_cfgparam() routine to set the
7150 * module lpfc_log_verbose into the @phba cfg_log_verbose for use with
7151 * log message according to the module's lpfc_log_verbose parameter setting
7152 * before hba port or vport created.
7153 **/
7154static void
7155lpfc_hba_log_verbose_init(struct lpfc_hba *phba, uint32_t verbose)
7156{
7157	phba->cfg_log_verbose = verbose;
7158}
7159
7160struct fc_function_template lpfc_transport_functions = {
7161	/* fixed attributes the driver supports */
7162	.show_host_node_name = 1,
7163	.show_host_port_name = 1,
7164	.show_host_supported_classes = 1,
7165	.show_host_supported_fc4s = 1,
7166	.show_host_supported_speeds = 1,
7167	.show_host_maxframe_size = 1,
7168
7169	.get_host_symbolic_name = lpfc_get_host_symbolic_name,
7170	.show_host_symbolic_name = 1,
7171
7172	/* dynamic attributes the driver supports */
7173	.get_host_port_id = lpfc_get_host_port_id,
7174	.show_host_port_id = 1,
7175
7176	.get_host_port_type = lpfc_get_host_port_type,
7177	.show_host_port_type = 1,
7178
7179	.get_host_port_state = lpfc_get_host_port_state,
7180	.show_host_port_state = 1,
7181
7182	/* active_fc4s is shown but doesn't change (thus no get function) */
7183	.show_host_active_fc4s = 1,
7184
7185	.get_host_speed = lpfc_get_host_speed,
7186	.show_host_speed = 1,
7187
7188	.get_host_fabric_name = lpfc_get_host_fabric_name,
7189	.show_host_fabric_name = 1,
7190
7191	/*
7192	 * The LPFC driver treats linkdown handling as target loss events
7193	 * so there are no sysfs handlers for link_down_tmo.
7194	 */
7195
7196	.get_fc_host_stats = lpfc_get_stats,
7197	.reset_fc_host_stats = lpfc_reset_stats,
7198
7199	.dd_fcrport_size = sizeof(struct lpfc_rport_data),
7200	.show_rport_maxframe_size = 1,
7201	.show_rport_supported_classes = 1,
7202
7203	.set_rport_dev_loss_tmo = lpfc_set_rport_loss_tmo,
7204	.show_rport_dev_loss_tmo = 1,
7205
7206	.get_starget_port_id  = lpfc_get_starget_port_id,
7207	.show_starget_port_id = 1,
7208
7209	.get_starget_node_name = lpfc_get_starget_node_name,
7210	.show_starget_node_name = 1,
7211
7212	.get_starget_port_name = lpfc_get_starget_port_name,
7213	.show_starget_port_name = 1,
7214
7215	.issue_fc_host_lip = lpfc_issue_lip,
7216	.dev_loss_tmo_callbk = lpfc_dev_loss_tmo_callbk,
7217	.terminate_rport_io = lpfc_terminate_rport_io,
7218
7219	.dd_fcvport_size = sizeof(struct lpfc_vport *),
7220
7221	.vport_disable = lpfc_vport_disable,
7222
7223	.set_vport_symbolic_name = lpfc_set_vport_symbolic_name,
7224
7225	.bsg_request = lpfc_bsg_request,
7226	.bsg_timeout = lpfc_bsg_timeout,
7227};
7228
7229struct fc_function_template lpfc_vport_transport_functions = {
7230	/* fixed attributes the driver supports */
7231	.show_host_node_name = 1,
7232	.show_host_port_name = 1,
7233	.show_host_supported_classes = 1,
7234	.show_host_supported_fc4s = 1,
7235	.show_host_supported_speeds = 1,
7236	.show_host_maxframe_size = 1,
7237
7238	.get_host_symbolic_name = lpfc_get_host_symbolic_name,
7239	.show_host_symbolic_name = 1,
7240
7241	/* dynamic attributes the driver supports */
7242	.get_host_port_id = lpfc_get_host_port_id,
7243	.show_host_port_id = 1,
7244
7245	.get_host_port_type = lpfc_get_host_port_type,
7246	.show_host_port_type = 1,
7247
7248	.get_host_port_state = lpfc_get_host_port_state,
7249	.show_host_port_state = 1,
7250
7251	/* active_fc4s is shown but doesn't change (thus no get function) */
7252	.show_host_active_fc4s = 1,
7253
7254	.get_host_speed = lpfc_get_host_speed,
7255	.show_host_speed = 1,
7256
7257	.get_host_fabric_name = lpfc_get_host_fabric_name,
7258	.show_host_fabric_name = 1,
7259
7260	/*
7261	 * The LPFC driver treats linkdown handling as target loss events
7262	 * so there are no sysfs handlers for link_down_tmo.
7263	 */
7264
7265	.get_fc_host_stats = lpfc_get_stats,
7266	.reset_fc_host_stats = lpfc_reset_stats,
7267
7268	.dd_fcrport_size = sizeof(struct lpfc_rport_data),
7269	.show_rport_maxframe_size = 1,
7270	.show_rport_supported_classes = 1,
7271
7272	.set_rport_dev_loss_tmo = lpfc_set_rport_loss_tmo,
7273	.show_rport_dev_loss_tmo = 1,
7274
7275	.get_starget_port_id  = lpfc_get_starget_port_id,
7276	.show_starget_port_id = 1,
7277
7278	.get_starget_node_name = lpfc_get_starget_node_name,
7279	.show_starget_node_name = 1,
7280
7281	.get_starget_port_name = lpfc_get_starget_port_name,
7282	.show_starget_port_name = 1,
7283
7284	.dev_loss_tmo_callbk = lpfc_dev_loss_tmo_callbk,
7285	.terminate_rport_io = lpfc_terminate_rport_io,
7286
7287	.vport_disable = lpfc_vport_disable,
7288
7289	.set_vport_symbolic_name = lpfc_set_vport_symbolic_name,
7290};
7291
7292/**
7293 * lpfc_get_hba_function_mode - Used to determine the HBA function in FCoE
7294 * Mode
7295 * @phba: lpfc_hba pointer.
7296 **/
7297static void
7298lpfc_get_hba_function_mode(struct lpfc_hba *phba)
7299{
7300	/* If the adapter supports FCoE mode */
7301	switch (phba->pcidev->device) {
7302	case PCI_DEVICE_ID_SKYHAWK:
7303	case PCI_DEVICE_ID_SKYHAWK_VF:
7304	case PCI_DEVICE_ID_LANCER_FCOE:
7305	case PCI_DEVICE_ID_LANCER_FCOE_VF:
7306	case PCI_DEVICE_ID_ZEPHYR_DCSP:
7307	case PCI_DEVICE_ID_TIGERSHARK:
7308	case PCI_DEVICE_ID_TOMCAT:
7309		set_bit(HBA_FCOE_MODE, &phba->hba_flag);
7310		break;
7311	default:
7312	/* for others, clear the flag */
7313		clear_bit(HBA_FCOE_MODE, &phba->hba_flag);
7314	}
7315}
7316
7317/**
7318 * lpfc_get_cfgparam - Used during probe_one to init the adapter structure
7319 * @phba: lpfc_hba pointer.
7320 **/
7321void
7322lpfc_get_cfgparam(struct lpfc_hba *phba)
7323{
7324	lpfc_hba_log_verbose_init(phba, lpfc_log_verbose);
7325	lpfc_fcp_io_sched_init(phba, lpfc_fcp_io_sched);
7326	lpfc_ns_query_init(phba, lpfc_ns_query);
7327	lpfc_fcp2_no_tgt_reset_init(phba, lpfc_fcp2_no_tgt_reset);
7328	lpfc_cr_delay_init(phba, lpfc_cr_delay);
7329	lpfc_cr_count_init(phba, lpfc_cr_count);
7330	lpfc_multi_ring_support_init(phba, lpfc_multi_ring_support);
7331	lpfc_multi_ring_rctl_init(phba, lpfc_multi_ring_rctl);
7332	lpfc_multi_ring_type_init(phba, lpfc_multi_ring_type);
7333	lpfc_ack0_init(phba, lpfc_ack0);
7334	lpfc_xri_rebalancing_init(phba, lpfc_xri_rebalancing);
7335	lpfc_topology_init(phba, lpfc_topology);
7336	lpfc_link_speed_init(phba, lpfc_link_speed);
7337	lpfc_poll_tmo_init(phba, lpfc_poll_tmo);
7338	lpfc_task_mgmt_tmo_init(phba, lpfc_task_mgmt_tmo);
7339	lpfc_enable_npiv_init(phba, lpfc_enable_npiv);
7340	lpfc_fcf_failover_policy_init(phba, lpfc_fcf_failover_policy);
7341	lpfc_enable_rrq_init(phba, lpfc_enable_rrq);
7342	lpfc_fcp_wait_abts_rsp_init(phba, lpfc_fcp_wait_abts_rsp);
7343	lpfc_fdmi_on_init(phba, lpfc_fdmi_on);
7344	lpfc_enable_SmartSAN_init(phba, lpfc_enable_SmartSAN);
7345	lpfc_use_msi_init(phba, lpfc_use_msi);
7346	lpfc_nvme_oas_init(phba, lpfc_nvme_oas);
7347	lpfc_nvme_embed_cmd_init(phba, lpfc_nvme_embed_cmd);
7348	lpfc_fcp_imax_init(phba, lpfc_fcp_imax);
7349	lpfc_force_rscn_init(phba, lpfc_force_rscn);
7350	lpfc_cq_poll_threshold_init(phba, lpfc_cq_poll_threshold);
7351	lpfc_cq_max_proc_limit_init(phba, lpfc_cq_max_proc_limit);
7352	lpfc_fcp_cpu_map_init(phba, lpfc_fcp_cpu_map);
7353	lpfc_enable_hba_reset_init(phba, lpfc_enable_hba_reset);
7354	lpfc_enable_hba_heartbeat_init(phba, lpfc_enable_hba_heartbeat);
7355
7356	lpfc_EnableXLane_init(phba, lpfc_EnableXLane);
7357	/* VMID Inits */
7358	lpfc_max_vmid_init(phba, lpfc_max_vmid);
7359	lpfc_vmid_inactivity_timeout_init(phba, lpfc_vmid_inactivity_timeout);
7360	lpfc_vmid_app_header_init(phba, lpfc_vmid_app_header);
7361	lpfc_vmid_priority_tagging_init(phba, lpfc_vmid_priority_tagging);
7362	if (phba->sli_rev != LPFC_SLI_REV4)
7363		phba->cfg_EnableXLane = 0;
7364	lpfc_XLanePriority_init(phba, lpfc_XLanePriority);
7365
7366	memset(phba->cfg_oas_tgt_wwpn, 0, (8 * sizeof(uint8_t)));
7367	memset(phba->cfg_oas_vpt_wwpn, 0, (8 * sizeof(uint8_t)));
7368	phba->cfg_oas_lun_state = 0;
7369	phba->cfg_oas_lun_status = 0;
7370	phba->cfg_oas_flags = 0;
7371	phba->cfg_oas_priority = 0;
7372	lpfc_enable_bg_init(phba, lpfc_enable_bg);
7373	lpfc_prot_mask_init(phba, lpfc_prot_mask);
7374	lpfc_prot_guard_init(phba, lpfc_prot_guard);
7375	if (phba->sli_rev == LPFC_SLI_REV4)
7376		phba->cfg_poll = 0;
7377	else
7378		phba->cfg_poll = lpfc_poll;
7379
7380	/* Get the function mode */
7381	lpfc_get_hba_function_mode(phba);
7382
7383	/* BlockGuard allowed for FC only. */
7384	if (phba->cfg_enable_bg && test_bit(HBA_FCOE_MODE, &phba->hba_flag)) {
7385		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7386				"0581 BlockGuard feature not supported\n");
7387		/* If set, clear the BlockGuard support param */
7388		phba->cfg_enable_bg = 0;
7389	} else if (phba->cfg_enable_bg) {
7390		phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
7391	}
7392
7393	lpfc_suppress_rsp_init(phba, lpfc_suppress_rsp);
7394
7395	lpfc_enable_fc4_type_init(phba, lpfc_enable_fc4_type);
7396	lpfc_nvmet_mrq_init(phba, lpfc_nvmet_mrq);
7397	lpfc_nvmet_mrq_post_init(phba, lpfc_nvmet_mrq_post);
7398
7399	/* Initialize first burst. Target vs Initiator are different. */
7400	lpfc_nvme_enable_fb_init(phba, lpfc_nvme_enable_fb);
7401	lpfc_nvmet_fb_size_init(phba, lpfc_nvmet_fb_size);
7402	lpfc_fcp_mq_threshold_init(phba, lpfc_fcp_mq_threshold);
7403	lpfc_hdw_queue_init(phba, lpfc_hdw_queue);
7404	lpfc_irq_chann_init(phba, lpfc_irq_chann);
7405	lpfc_enable_bbcr_init(phba, lpfc_enable_bbcr);
7406	lpfc_enable_dpp_init(phba, lpfc_enable_dpp);
7407	lpfc_enable_mi_init(phba, lpfc_enable_mi);
7408
7409	phba->cgn_p.cgn_param_mode = LPFC_CFG_OFF;
7410	phba->cmf_active_mode = LPFC_CFG_OFF;
7411	if (lpfc_fabric_cgn_frequency > EDC_CG_SIGFREQ_CNT_MAX ||
7412	   lpfc_fabric_cgn_frequency < EDC_CG_SIGFREQ_CNT_MIN)
7413		lpfc_fabric_cgn_frequency = 100; /* 100 ms default */
7414
7415	if (phba->sli_rev != LPFC_SLI_REV4) {
7416		/* NVME only supported on SLI4 */
7417		phba->nvmet_support = 0;
7418		phba->cfg_nvmet_mrq = 0;
7419		phba->cfg_enable_fc4_type = LPFC_ENABLE_FCP;
7420		phba->cfg_enable_bbcr = 0;
7421		phba->cfg_xri_rebalancing = 0;
7422	} else {
7423		/* We MUST have FCP support */
7424		if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
7425			phba->cfg_enable_fc4_type |= LPFC_ENABLE_FCP;
7426	}
7427
7428	phba->cfg_auto_imax = (phba->cfg_fcp_imax) ? 0 : 1;
7429
7430	phba->cfg_enable_pbde = 0;
7431
7432	/* A value of 0 means use the number of CPUs found in the system */
7433	if (phba->cfg_hdw_queue == 0)
7434		phba->cfg_hdw_queue = phba->sli4_hba.num_present_cpu;
7435	if (phba->cfg_irq_chann == 0)
7436		phba->cfg_irq_chann = phba->sli4_hba.num_present_cpu;
7437	if (phba->cfg_irq_chann > phba->cfg_hdw_queue &&
7438	    phba->sli_rev == LPFC_SLI_REV4)
7439		phba->cfg_irq_chann = phba->cfg_hdw_queue;
7440
7441	lpfc_sg_seg_cnt_init(phba, lpfc_sg_seg_cnt);
7442	lpfc_hba_queue_depth_init(phba, lpfc_hba_queue_depth);
7443	lpfc_sriov_nr_virtfn_init(phba, lpfc_sriov_nr_virtfn);
7444	lpfc_request_firmware_upgrade_init(phba, lpfc_req_fw_upgrade);
7445	lpfc_suppress_link_up_init(phba, lpfc_suppress_link_up);
7446	lpfc_delay_discovery_init(phba, lpfc_delay_discovery);
7447	lpfc_sli_mode_init(phba, lpfc_sli_mode);
7448	lpfc_enable_mds_diags_init(phba, lpfc_enable_mds_diags);
7449	lpfc_ras_fwlog_buffsize_init(phba, lpfc_ras_fwlog_buffsize);
7450	lpfc_ras_fwlog_level_init(phba, lpfc_ras_fwlog_level);
7451	lpfc_ras_fwlog_func_init(phba, lpfc_ras_fwlog_func);
7452
7453	return;
7454}
7455
7456/**
7457 * lpfc_nvme_mod_param_dep - Adjust module parameter value based on
7458 * dependencies between protocols and roles.
7459 * @phba: lpfc_hba pointer.
7460 **/
7461void
7462lpfc_nvme_mod_param_dep(struct lpfc_hba *phba)
7463{
7464	int  logit = 0;
7465
7466	if (phba->cfg_hdw_queue > phba->sli4_hba.num_present_cpu) {
7467		phba->cfg_hdw_queue = phba->sli4_hba.num_present_cpu;
7468		logit = 1;
7469	}
7470	if (phba->cfg_irq_chann > phba->sli4_hba.num_present_cpu) {
7471		phba->cfg_irq_chann = phba->sli4_hba.num_present_cpu;
7472		logit = 1;
7473	}
7474	if (phba->cfg_irq_chann > phba->cfg_hdw_queue) {
7475		phba->cfg_irq_chann = phba->cfg_hdw_queue;
7476		logit = 1;
7477	}
7478	if (logit)
7479		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7480				"2006 Reducing Queues - CPU limitation: "
7481				"IRQ %d HDWQ %d\n",
7482				phba->cfg_irq_chann,
7483				phba->cfg_hdw_queue);
7484
7485	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME &&
7486	    phba->nvmet_support) {
7487		phba->cfg_enable_fc4_type &= ~LPFC_ENABLE_FCP;
7488
7489		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
7490				"6013 %s x%x fb_size x%x, fb_max x%x\n",
7491				"NVME Target PRLI ACC enable_fb ",
7492				phba->cfg_nvme_enable_fb,
7493				phba->cfg_nvmet_fb_size,
7494				LPFC_NVMET_FB_SZ_MAX);
7495
7496		if (phba->cfg_nvme_enable_fb == 0)
7497			phba->cfg_nvmet_fb_size = 0;
7498		else {
7499			if (phba->cfg_nvmet_fb_size > LPFC_NVMET_FB_SZ_MAX)
7500				phba->cfg_nvmet_fb_size = LPFC_NVMET_FB_SZ_MAX;
7501		}
7502
7503		if (!phba->cfg_nvmet_mrq)
7504			phba->cfg_nvmet_mrq = phba->cfg_hdw_queue;
7505
7506		/* Adjust lpfc_nvmet_mrq to avoid running out of WQE slots */
7507		if (phba->cfg_nvmet_mrq > phba->cfg_hdw_queue) {
7508			phba->cfg_nvmet_mrq = phba->cfg_hdw_queue;
7509			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
7510					"6018 Adjust lpfc_nvmet_mrq to %d\n",
7511					phba->cfg_nvmet_mrq);
7512		}
7513		if (phba->cfg_nvmet_mrq > LPFC_NVMET_MRQ_MAX)
7514			phba->cfg_nvmet_mrq = LPFC_NVMET_MRQ_MAX;
7515
7516	} else {
7517		/* Not NVME Target mode.  Turn off Target parameters. */
7518		phba->nvmet_support = 0;
7519		phba->cfg_nvmet_mrq = 0;
7520		phba->cfg_nvmet_fb_size = 0;
7521	}
7522}
7523
7524/**
7525 * lpfc_get_vport_cfgparam - Used during port create, init the vport structure
7526 * @vport: lpfc_vport pointer.
7527 **/
7528void
7529lpfc_get_vport_cfgparam(struct lpfc_vport *vport)
7530{
7531	lpfc_log_verbose_init(vport, lpfc_log_verbose);
7532	lpfc_lun_queue_depth_init(vport, lpfc_lun_queue_depth);
7533	lpfc_tgt_queue_depth_init(vport, lpfc_tgt_queue_depth);
7534	lpfc_devloss_tmo_init(vport, lpfc_devloss_tmo);
7535	lpfc_nodev_tmo_init(vport, lpfc_nodev_tmo);
7536	lpfc_peer_port_login_init(vport, lpfc_peer_port_login);
7537	lpfc_restrict_login_init(vport, lpfc_restrict_login);
7538	lpfc_fcp_class_init(vport, lpfc_fcp_class);
7539	lpfc_use_adisc_init(vport, lpfc_use_adisc);
7540	lpfc_first_burst_size_init(vport, lpfc_first_burst_size);
7541	lpfc_max_scsicmpl_time_init(vport, lpfc_max_scsicmpl_time);
7542	lpfc_discovery_threads_init(vport, lpfc_discovery_threads);
7543	lpfc_max_luns_init(vport, lpfc_max_luns);
7544	lpfc_scan_down_init(vport, lpfc_scan_down);
7545	lpfc_enable_da_id_init(vport, lpfc_enable_da_id);
7546	return;
7547}
~/ Read-only file preview.