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