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