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