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