xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_attr.c (revision bb0eb050)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017 Broadcom. All Rights Reserved. The term      *
5  * “Broadcom” refers to Broadcom Limited 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 <linux/nvme-fc-driver.h>
41 
42 #include "lpfc_hw4.h"
43 #include "lpfc_hw.h"
44 #include "lpfc_sli.h"
45 #include "lpfc_sli4.h"
46 #include "lpfc_nl.h"
47 #include "lpfc_disc.h"
48 #include "lpfc.h"
49 #include "lpfc_scsi.h"
50 #include "lpfc_nvme.h"
51 #include "lpfc_nvmet.h"
52 #include "lpfc_logmsg.h"
53 #include "lpfc_version.h"
54 #include "lpfc_compat.h"
55 #include "lpfc_crtn.h"
56 #include "lpfc_vport.h"
57 #include "lpfc_attr.h"
58 
59 #define LPFC_DEF_DEVLOSS_TMO	30
60 #define LPFC_MIN_DEVLOSS_TMO	1
61 #define LPFC_MAX_DEVLOSS_TMO	255
62 
63 #define LPFC_DEF_MRQ_POST	512
64 #define LPFC_MIN_MRQ_POST	512
65 #define LPFC_MAX_MRQ_POST	2048
66 
67 /*
68  * Write key size should be multiple of 4. If write key is changed
69  * make sure that library write key is also changed.
70  */
71 #define LPFC_REG_WRITE_KEY_SIZE	4
72 #define LPFC_REG_WRITE_KEY	"EMLX"
73 
74 /**
75  * lpfc_jedec_to_ascii - Hex to ascii convertor according to JEDEC rules
76  * @incr: integer to convert.
77  * @hdw: ascii string holding converted integer plus a string terminator.
78  *
79  * Description:
80  * JEDEC Joint Electron Device Engineering Council.
81  * Convert a 32 bit integer composed of 8 nibbles into an 8 byte ascii
82  * character string. The string is then terminated with a NULL in byte 9.
83  * Hex 0-9 becomes ascii '0' to '9'.
84  * Hex a-f becomes ascii '=' to 'B' capital B.
85  *
86  * Notes:
87  * Coded for 32 bit integers only.
88  **/
89 static void
90 lpfc_jedec_to_ascii(int incr, char hdw[])
91 {
92 	int i, j;
93 	for (i = 0; i < 8; i++) {
94 		j = (incr & 0xf);
95 		if (j <= 9)
96 			hdw[7 - i] = 0x30 +  j;
97 		 else
98 			hdw[7 - i] = 0x61 + j - 10;
99 		incr = (incr >> 4);
100 	}
101 	hdw[8] = 0;
102 	return;
103 }
104 
105 /**
106  * lpfc_drvr_version_show - Return the Emulex driver string with version number
107  * @dev: class unused variable.
108  * @attr: device attribute, not used.
109  * @buf: on return contains the module description text.
110  *
111  * Returns: size of formatted string.
112  **/
113 static ssize_t
114 lpfc_drvr_version_show(struct device *dev, struct device_attribute *attr,
115 		       char *buf)
116 {
117 	return snprintf(buf, PAGE_SIZE, LPFC_MODULE_DESC "\n");
118 }
119 
120 /**
121  * lpfc_enable_fip_show - Return the fip mode of the HBA
122  * @dev: class unused variable.
123  * @attr: device attribute, not used.
124  * @buf: on return contains the module description text.
125  *
126  * Returns: size of formatted string.
127  **/
128 static ssize_t
129 lpfc_enable_fip_show(struct device *dev, struct device_attribute *attr,
130 		       char *buf)
131 {
132 	struct Scsi_Host *shost = class_to_shost(dev);
133 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
134 	struct lpfc_hba   *phba = vport->phba;
135 
136 	if (phba->hba_flag & HBA_FIP_SUPPORT)
137 		return snprintf(buf, PAGE_SIZE, "1\n");
138 	else
139 		return snprintf(buf, PAGE_SIZE, "0\n");
140 }
141 
142 static ssize_t
143 lpfc_nvme_info_show(struct device *dev, struct device_attribute *attr,
144 		    char *buf)
145 {
146 	struct Scsi_Host *shost = class_to_shost(dev);
147 	struct lpfc_vport *vport = shost_priv(shost);
148 	struct lpfc_hba   *phba = vport->phba;
149 	struct lpfc_nvmet_tgtport *tgtp;
150 	struct nvme_fc_local_port *localport;
151 	struct lpfc_nvme_lport *lport;
152 	struct lpfc_nvme_rport *rport;
153 	struct nvme_fc_remote_port *nrport;
154 	char *statep;
155 	int len = 0;
156 
157 	if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)) {
158 		len += snprintf(buf, PAGE_SIZE, "NVME Disabled\n");
159 		return len;
160 	}
161 	if (phba->nvmet_support) {
162 		if (!phba->targetport) {
163 			len = snprintf(buf, PAGE_SIZE,
164 					"NVME Target: x%llx is not allocated\n",
165 					wwn_to_u64(vport->fc_portname.u.wwn));
166 			return len;
167 		}
168 		/* Port state is only one of two values for now. */
169 		if (phba->targetport->port_id)
170 			statep = "REGISTERED";
171 		else
172 			statep = "INIT";
173 		len += snprintf(buf + len, PAGE_SIZE - len,
174 				"NVME Target: Enabled  State %s\n",
175 				statep);
176 		len += snprintf(buf + len, PAGE_SIZE - len,
177 				"%s%d WWPN x%llx WWNN x%llx DID x%06x\n",
178 				"NVME Target: lpfc",
179 				phba->brd_no,
180 				wwn_to_u64(vport->fc_portname.u.wwn),
181 				wwn_to_u64(vport->fc_nodename.u.wwn),
182 				phba->targetport->port_id);
183 
184 		len += snprintf(buf + len, PAGE_SIZE - len,
185 				"\nNVME Target: Statistics\n");
186 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
187 		len += snprintf(buf+len, PAGE_SIZE-len,
188 				"LS: Rcv %08x Drop %08x Abort %08x\n",
189 				atomic_read(&tgtp->rcv_ls_req_in),
190 				atomic_read(&tgtp->rcv_ls_req_drop),
191 				atomic_read(&tgtp->xmt_ls_abort));
192 		if (atomic_read(&tgtp->rcv_ls_req_in) !=
193 		    atomic_read(&tgtp->rcv_ls_req_out)) {
194 			len += snprintf(buf+len, PAGE_SIZE-len,
195 					"Rcv LS: in %08x != out %08x\n",
196 					atomic_read(&tgtp->rcv_ls_req_in),
197 					atomic_read(&tgtp->rcv_ls_req_out));
198 		}
199 
200 		len += snprintf(buf+len, PAGE_SIZE-len,
201 				"LS: Xmt %08x Drop %08x Cmpl %08x Err %08x\n",
202 				atomic_read(&tgtp->xmt_ls_rsp),
203 				atomic_read(&tgtp->xmt_ls_drop),
204 				atomic_read(&tgtp->xmt_ls_rsp_cmpl),
205 				atomic_read(&tgtp->xmt_ls_rsp_error));
206 
207 		len += snprintf(buf+len, PAGE_SIZE-len,
208 				"FCP: Rcv %08x Release %08x Drop %08x\n",
209 				atomic_read(&tgtp->rcv_fcp_cmd_in),
210 				atomic_read(&tgtp->xmt_fcp_release),
211 				atomic_read(&tgtp->rcv_fcp_cmd_drop));
212 
213 		if (atomic_read(&tgtp->rcv_fcp_cmd_in) !=
214 		    atomic_read(&tgtp->rcv_fcp_cmd_out)) {
215 			len += snprintf(buf+len, PAGE_SIZE-len,
216 					"Rcv FCP: in %08x != out %08x\n",
217 					atomic_read(&tgtp->rcv_fcp_cmd_in),
218 					atomic_read(&tgtp->rcv_fcp_cmd_out));
219 		}
220 
221 		len += snprintf(buf+len, PAGE_SIZE-len,
222 				"FCP Rsp: RD %08x rsp %08x WR %08x rsp %08x "
223 				"drop %08x\n",
224 				atomic_read(&tgtp->xmt_fcp_read),
225 				atomic_read(&tgtp->xmt_fcp_read_rsp),
226 				atomic_read(&tgtp->xmt_fcp_write),
227 				atomic_read(&tgtp->xmt_fcp_rsp),
228 				atomic_read(&tgtp->xmt_fcp_drop));
229 
230 		len += snprintf(buf+len, PAGE_SIZE-len,
231 				"FCP Rsp Cmpl: %08x err %08x drop %08x\n",
232 				atomic_read(&tgtp->xmt_fcp_rsp_cmpl),
233 				atomic_read(&tgtp->xmt_fcp_rsp_error),
234 				atomic_read(&tgtp->xmt_fcp_rsp_drop));
235 
236 		len += snprintf(buf+len, PAGE_SIZE-len,
237 				"ABORT: Xmt %08x Cmpl %08x\n",
238 				atomic_read(&tgtp->xmt_fcp_abort),
239 				atomic_read(&tgtp->xmt_fcp_abort_cmpl));
240 
241 		len += snprintf(buf + len, PAGE_SIZE - len,
242 				"ABORT: Sol %08x  Usol %08x Err %08x Cmpl %08x",
243 				atomic_read(&tgtp->xmt_abort_sol),
244 				atomic_read(&tgtp->xmt_abort_unsol),
245 				atomic_read(&tgtp->xmt_abort_rsp),
246 				atomic_read(&tgtp->xmt_abort_rsp_error));
247 
248 		len += snprintf(buf + len, PAGE_SIZE - len,
249 				"IO_CTX: %08x outstanding %08x total %x",
250 				phba->sli4_hba.nvmet_ctx_cnt,
251 				phba->sli4_hba.nvmet_io_wait_cnt,
252 				phba->sli4_hba.nvmet_io_wait_total);
253 
254 		len +=  snprintf(buf+len, PAGE_SIZE-len, "\n");
255 		return len;
256 	}
257 
258 	localport = vport->localport;
259 	if (!localport) {
260 		len = snprintf(buf, PAGE_SIZE,
261 				"NVME Initiator x%llx is not allocated\n",
262 				wwn_to_u64(vport->fc_portname.u.wwn));
263 		return len;
264 	}
265 	len = snprintf(buf, PAGE_SIZE, "NVME Initiator Enabled\n");
266 
267 	spin_lock_irq(shost->host_lock);
268 	lport = (struct lpfc_nvme_lport *)localport->private;
269 
270 	/* Port state is only one of two values for now. */
271 	if (localport->port_id)
272 		statep = "ONLINE";
273 	else
274 		statep = "UNKNOWN ";
275 
276 	len += snprintf(buf + len, PAGE_SIZE - len,
277 			"%s%d WWPN x%llx WWNN x%llx DID x%06x %s\n",
278 			"NVME LPORT lpfc",
279 			phba->brd_no,
280 			wwn_to_u64(vport->fc_portname.u.wwn),
281 			wwn_to_u64(vport->fc_nodename.u.wwn),
282 			localport->port_id, statep);
283 
284 	list_for_each_entry(rport, &lport->rport_list, list) {
285 		/* local short-hand pointer. */
286 		nrport = rport->remoteport;
287 
288 		/* Port state is only one of two values for now. */
289 		switch (nrport->port_state) {
290 		case FC_OBJSTATE_ONLINE:
291 			statep = "ONLINE";
292 			break;
293 		case FC_OBJSTATE_UNKNOWN:
294 			statep = "UNKNOWN ";
295 			break;
296 		default:
297 			statep = "UNSUPPORTED";
298 			break;
299 		}
300 
301 		/* Tab in to show lport ownership. */
302 		len += snprintf(buf + len, PAGE_SIZE - len,
303 				"NVME RPORT       ");
304 		if (phba->brd_no >= 10)
305 			len += snprintf(buf + len, PAGE_SIZE - len, " ");
306 
307 		len += snprintf(buf + len, PAGE_SIZE - len, "WWPN x%llx ",
308 				nrport->port_name);
309 		len += snprintf(buf + len, PAGE_SIZE - len, "WWNN x%llx ",
310 				nrport->node_name);
311 		len += snprintf(buf + len, PAGE_SIZE - len, "DID x%06x ",
312 				nrport->port_id);
313 
314 		switch (nrport->port_role) {
315 		case FC_PORT_ROLE_NVME_INITIATOR:
316 			len +=  snprintf(buf + len, PAGE_SIZE - len,
317 					 "INITIATOR ");
318 			break;
319 		case FC_PORT_ROLE_NVME_TARGET:
320 			len +=  snprintf(buf + len, PAGE_SIZE - len,
321 					 "TARGET ");
322 			break;
323 		case FC_PORT_ROLE_NVME_DISCOVERY:
324 			len +=  snprintf(buf + len, PAGE_SIZE - len,
325 					 "DISCOVERY ");
326 			break;
327 		default:
328 			len +=  snprintf(buf + len, PAGE_SIZE - len,
329 					 "UNKNOWN_ROLE x%x",
330 					 nrport->port_role);
331 			break;
332 		}
333 		len +=  snprintf(buf + len, PAGE_SIZE - len, "%s  ", statep);
334 		/* Terminate the string. */
335 		len +=  snprintf(buf + len, PAGE_SIZE - len, "\n");
336 	}
337 	spin_unlock_irq(shost->host_lock);
338 
339 	len += snprintf(buf + len, PAGE_SIZE - len, "\nNVME Statistics\n");
340 	len += snprintf(buf+len, PAGE_SIZE-len,
341 			"LS: Xmt %016llx Cmpl %016llx\n",
342 			phba->fc4NvmeLsRequests,
343 			phba->fc4NvmeLsCmpls);
344 
345 	len += snprintf(buf+len, PAGE_SIZE-len,
346 			"FCP: Rd %016llx Wr %016llx IO %016llx\n",
347 			phba->fc4NvmeInputRequests,
348 			phba->fc4NvmeOutputRequests,
349 			phba->fc4NvmeControlRequests);
350 
351 	len += snprintf(buf+len, PAGE_SIZE-len,
352 			"    Cmpl %016llx\n", phba->fc4NvmeIoCmpls);
353 
354 	return len;
355 }
356 
357 static ssize_t
358 lpfc_bg_info_show(struct device *dev, struct device_attribute *attr,
359 		  char *buf)
360 {
361 	struct Scsi_Host *shost = class_to_shost(dev);
362 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
363 	struct lpfc_hba   *phba = vport->phba;
364 
365 	if (phba->cfg_enable_bg)
366 		if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
367 			return snprintf(buf, PAGE_SIZE, "BlockGuard Enabled\n");
368 		else
369 			return snprintf(buf, PAGE_SIZE,
370 					"BlockGuard Not Supported\n");
371 	else
372 			return snprintf(buf, PAGE_SIZE,
373 					"BlockGuard Disabled\n");
374 }
375 
376 static ssize_t
377 lpfc_bg_guard_err_show(struct device *dev, struct device_attribute *attr,
378 		       char *buf)
379 {
380 	struct Scsi_Host *shost = class_to_shost(dev);
381 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
382 	struct lpfc_hba   *phba = vport->phba;
383 
384 	return snprintf(buf, PAGE_SIZE, "%llu\n",
385 			(unsigned long long)phba->bg_guard_err_cnt);
386 }
387 
388 static ssize_t
389 lpfc_bg_apptag_err_show(struct device *dev, struct device_attribute *attr,
390 			char *buf)
391 {
392 	struct Scsi_Host *shost = class_to_shost(dev);
393 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
394 	struct lpfc_hba   *phba = vport->phba;
395 
396 	return snprintf(buf, PAGE_SIZE, "%llu\n",
397 			(unsigned long long)phba->bg_apptag_err_cnt);
398 }
399 
400 static ssize_t
401 lpfc_bg_reftag_err_show(struct device *dev, struct device_attribute *attr,
402 			char *buf)
403 {
404 	struct Scsi_Host *shost = class_to_shost(dev);
405 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
406 	struct lpfc_hba   *phba = vport->phba;
407 
408 	return snprintf(buf, PAGE_SIZE, "%llu\n",
409 			(unsigned long long)phba->bg_reftag_err_cnt);
410 }
411 
412 /**
413  * lpfc_info_show - Return some pci info about the host in ascii
414  * @dev: class converted to a Scsi_host structure.
415  * @attr: device attribute, not used.
416  * @buf: on return contains the formatted text from lpfc_info().
417  *
418  * Returns: size of formatted string.
419  **/
420 static ssize_t
421 lpfc_info_show(struct device *dev, struct device_attribute *attr,
422 	       char *buf)
423 {
424 	struct Scsi_Host *host = class_to_shost(dev);
425 
426 	return snprintf(buf, PAGE_SIZE, "%s\n",lpfc_info(host));
427 }
428 
429 /**
430  * lpfc_serialnum_show - Return the hba serial number in ascii
431  * @dev: class converted to a Scsi_host structure.
432  * @attr: device attribute, not used.
433  * @buf: on return contains the formatted text serial number.
434  *
435  * Returns: size of formatted string.
436  **/
437 static ssize_t
438 lpfc_serialnum_show(struct device *dev, struct device_attribute *attr,
439 		    char *buf)
440 {
441 	struct Scsi_Host  *shost = class_to_shost(dev);
442 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
443 	struct lpfc_hba   *phba = vport->phba;
444 
445 	return snprintf(buf, PAGE_SIZE, "%s\n",phba->SerialNumber);
446 }
447 
448 /**
449  * lpfc_temp_sensor_show - Return the temperature sensor level
450  * @dev: class converted to a Scsi_host structure.
451  * @attr: device attribute, not used.
452  * @buf: on return contains the formatted support level.
453  *
454  * Description:
455  * Returns a number indicating the temperature sensor level currently
456  * supported, zero or one in ascii.
457  *
458  * Returns: size of formatted string.
459  **/
460 static ssize_t
461 lpfc_temp_sensor_show(struct device *dev, struct device_attribute *attr,
462 		      char *buf)
463 {
464 	struct Scsi_Host *shost = class_to_shost(dev);
465 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
466 	struct lpfc_hba   *phba = vport->phba;
467 	return snprintf(buf, PAGE_SIZE, "%d\n",phba->temp_sensor_support);
468 }
469 
470 /**
471  * lpfc_modeldesc_show - Return the model description of the hba
472  * @dev: class converted to a Scsi_host structure.
473  * @attr: device attribute, not used.
474  * @buf: on return contains the scsi vpd model description.
475  *
476  * Returns: size of formatted string.
477  **/
478 static ssize_t
479 lpfc_modeldesc_show(struct device *dev, struct device_attribute *attr,
480 		    char *buf)
481 {
482 	struct Scsi_Host  *shost = class_to_shost(dev);
483 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
484 	struct lpfc_hba   *phba = vport->phba;
485 
486 	return snprintf(buf, PAGE_SIZE, "%s\n",phba->ModelDesc);
487 }
488 
489 /**
490  * lpfc_modelname_show - Return the model name of the hba
491  * @dev: class converted to a Scsi_host structure.
492  * @attr: device attribute, not used.
493  * @buf: on return contains the scsi vpd model name.
494  *
495  * Returns: size of formatted string.
496  **/
497 static ssize_t
498 lpfc_modelname_show(struct device *dev, struct device_attribute *attr,
499 		    char *buf)
500 {
501 	struct Scsi_Host  *shost = class_to_shost(dev);
502 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
503 	struct lpfc_hba   *phba = vport->phba;
504 
505 	return snprintf(buf, PAGE_SIZE, "%s\n",phba->ModelName);
506 }
507 
508 /**
509  * lpfc_programtype_show - Return the program type of the hba
510  * @dev: class converted to a Scsi_host structure.
511  * @attr: device attribute, not used.
512  * @buf: on return contains the scsi vpd program type.
513  *
514  * Returns: size of formatted string.
515  **/
516 static ssize_t
517 lpfc_programtype_show(struct device *dev, struct device_attribute *attr,
518 		      char *buf)
519 {
520 	struct Scsi_Host  *shost = class_to_shost(dev);
521 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
522 	struct lpfc_hba   *phba = vport->phba;
523 
524 	return snprintf(buf, PAGE_SIZE, "%s\n",phba->ProgramType);
525 }
526 
527 /**
528  * lpfc_mlomgmt_show - Return the Menlo Maintenance sli flag
529  * @dev: class converted to a Scsi_host structure.
530  * @attr: device attribute, not used.
531  * @buf: on return contains the Menlo Maintenance sli flag.
532  *
533  * Returns: size of formatted string.
534  **/
535 static ssize_t
536 lpfc_mlomgmt_show(struct device *dev, struct device_attribute *attr, char *buf)
537 {
538 	struct Scsi_Host  *shost = class_to_shost(dev);
539 	struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
540 	struct lpfc_hba   *phba = vport->phba;
541 
542 	return snprintf(buf, PAGE_SIZE, "%d\n",
543 		(phba->sli.sli_flag & LPFC_MENLO_MAINT));
544 }
545 
546 /**
547  * lpfc_vportnum_show - Return the port number in ascii of the hba
548  * @dev: class converted to a Scsi_host structure.
549  * @attr: device attribute, not used.
550  * @buf: on return contains scsi vpd program type.
551  *
552  * Returns: size of formatted string.
553  **/
554 static ssize_t
555 lpfc_vportnum_show(struct device *dev, struct device_attribute *attr,
556 		   char *buf)
557 {
558 	struct Scsi_Host  *shost = class_to_shost(dev);
559 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
560 	struct lpfc_hba   *phba = vport->phba;
561 
562 	return snprintf(buf, PAGE_SIZE, "%s\n",phba->Port);
563 }
564 
565 /**
566  * lpfc_fwrev_show - Return the firmware rev running in the hba
567  * @dev: class converted to a Scsi_host structure.
568  * @attr: device attribute, not used.
569  * @buf: on return contains the scsi vpd program type.
570  *
571  * Returns: size of formatted string.
572  **/
573 static ssize_t
574 lpfc_fwrev_show(struct device *dev, struct device_attribute *attr,
575 		char *buf)
576 {
577 	struct Scsi_Host  *shost = class_to_shost(dev);
578 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
579 	struct lpfc_hba   *phba = vport->phba;
580 	uint32_t if_type;
581 	uint8_t sli_family;
582 	char fwrev[FW_REV_STR_SIZE];
583 	int len;
584 
585 	lpfc_decode_firmware_rev(phba, fwrev, 1);
586 	if_type = phba->sli4_hba.pc_sli4_params.if_type;
587 	sli_family = phba->sli4_hba.pc_sli4_params.sli_family;
588 
589 	if (phba->sli_rev < LPFC_SLI_REV4)
590 		len = snprintf(buf, PAGE_SIZE, "%s, sli-%d\n",
591 			       fwrev, phba->sli_rev);
592 	else
593 		len = snprintf(buf, PAGE_SIZE, "%s, sli-%d:%d:%x\n",
594 			       fwrev, phba->sli_rev, if_type, sli_family);
595 
596 	return len;
597 }
598 
599 /**
600  * lpfc_hdw_show - Return the jedec information about the hba
601  * @dev: class converted to a Scsi_host structure.
602  * @attr: device attribute, not used.
603  * @buf: on return contains the scsi vpd program type.
604  *
605  * Returns: size of formatted string.
606  **/
607 static ssize_t
608 lpfc_hdw_show(struct device *dev, struct device_attribute *attr, char *buf)
609 {
610 	char hdw[9];
611 	struct Scsi_Host  *shost = class_to_shost(dev);
612 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
613 	struct lpfc_hba   *phba = vport->phba;
614 	lpfc_vpd_t *vp = &phba->vpd;
615 
616 	lpfc_jedec_to_ascii(vp->rev.biuRev, hdw);
617 	return snprintf(buf, PAGE_SIZE, "%s\n", hdw);
618 }
619 
620 /**
621  * lpfc_option_rom_version_show - Return the adapter ROM FCode version
622  * @dev: class converted to a Scsi_host structure.
623  * @attr: device attribute, not used.
624  * @buf: on return contains the ROM and FCode ascii strings.
625  *
626  * Returns: size of formatted string.
627  **/
628 static ssize_t
629 lpfc_option_rom_version_show(struct device *dev, struct device_attribute *attr,
630 			     char *buf)
631 {
632 	struct Scsi_Host  *shost = class_to_shost(dev);
633 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
634 	struct lpfc_hba   *phba = vport->phba;
635 	char fwrev[FW_REV_STR_SIZE];
636 
637 	if (phba->sli_rev < LPFC_SLI_REV4)
638 		return snprintf(buf, PAGE_SIZE, "%s\n", phba->OptionROMVersion);
639 
640 	lpfc_decode_firmware_rev(phba, fwrev, 1);
641 	return snprintf(buf, PAGE_SIZE, "%s\n", fwrev);
642 }
643 
644 /**
645  * lpfc_state_show - Return the link state of the port
646  * @dev: class converted to a Scsi_host structure.
647  * @attr: device attribute, not used.
648  * @buf: on return contains text describing the state of the link.
649  *
650  * Notes:
651  * The switch statement has no default so zero will be returned.
652  *
653  * Returns: size of formatted string.
654  **/
655 static ssize_t
656 lpfc_link_state_show(struct device *dev, struct device_attribute *attr,
657 		     char *buf)
658 {
659 	struct Scsi_Host  *shost = class_to_shost(dev);
660 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
661 	struct lpfc_hba   *phba = vport->phba;
662 	int  len = 0;
663 
664 	switch (phba->link_state) {
665 	case LPFC_LINK_UNKNOWN:
666 	case LPFC_WARM_START:
667 	case LPFC_INIT_START:
668 	case LPFC_INIT_MBX_CMDS:
669 	case LPFC_LINK_DOWN:
670 	case LPFC_HBA_ERROR:
671 		if (phba->hba_flag & LINK_DISABLED)
672 			len += snprintf(buf + len, PAGE_SIZE-len,
673 				"Link Down - User disabled\n");
674 		else
675 			len += snprintf(buf + len, PAGE_SIZE-len,
676 				"Link Down\n");
677 		break;
678 	case LPFC_LINK_UP:
679 	case LPFC_CLEAR_LA:
680 	case LPFC_HBA_READY:
681 		len += snprintf(buf + len, PAGE_SIZE-len, "Link Up - ");
682 
683 		switch (vport->port_state) {
684 		case LPFC_LOCAL_CFG_LINK:
685 			len += snprintf(buf + len, PAGE_SIZE-len,
686 					"Configuring Link\n");
687 			break;
688 		case LPFC_FDISC:
689 		case LPFC_FLOGI:
690 		case LPFC_FABRIC_CFG_LINK:
691 		case LPFC_NS_REG:
692 		case LPFC_NS_QRY:
693 		case LPFC_BUILD_DISC_LIST:
694 		case LPFC_DISC_AUTH:
695 			len += snprintf(buf + len, PAGE_SIZE - len,
696 					"Discovery\n");
697 			break;
698 		case LPFC_VPORT_READY:
699 			len += snprintf(buf + len, PAGE_SIZE - len, "Ready\n");
700 			break;
701 
702 		case LPFC_VPORT_FAILED:
703 			len += snprintf(buf + len, PAGE_SIZE - len, "Failed\n");
704 			break;
705 
706 		case LPFC_VPORT_UNKNOWN:
707 			len += snprintf(buf + len, PAGE_SIZE - len,
708 					"Unknown\n");
709 			break;
710 		}
711 		if (phba->sli.sli_flag & LPFC_MENLO_MAINT)
712 			len += snprintf(buf + len, PAGE_SIZE-len,
713 					"   Menlo Maint Mode\n");
714 		else if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
715 			if (vport->fc_flag & FC_PUBLIC_LOOP)
716 				len += snprintf(buf + len, PAGE_SIZE-len,
717 						"   Public Loop\n");
718 			else
719 				len += snprintf(buf + len, PAGE_SIZE-len,
720 						"   Private Loop\n");
721 		} else {
722 			if (vport->fc_flag & FC_FABRIC)
723 				len += snprintf(buf + len, PAGE_SIZE-len,
724 						"   Fabric\n");
725 			else
726 				len += snprintf(buf + len, PAGE_SIZE-len,
727 						"   Point-2-Point\n");
728 		}
729 	}
730 
731 	return len;
732 }
733 
734 /**
735  * lpfc_sli4_protocol_show - Return the fip mode of the HBA
736  * @dev: class unused variable.
737  * @attr: device attribute, not used.
738  * @buf: on return contains the module description text.
739  *
740  * Returns: size of formatted string.
741  **/
742 static ssize_t
743 lpfc_sli4_protocol_show(struct device *dev, struct device_attribute *attr,
744 			char *buf)
745 {
746 	struct Scsi_Host *shost = class_to_shost(dev);
747 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
748 	struct lpfc_hba *phba = vport->phba;
749 
750 	if (phba->sli_rev < LPFC_SLI_REV4)
751 		return snprintf(buf, PAGE_SIZE, "fc\n");
752 
753 	if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL) {
754 		if (phba->sli4_hba.lnk_info.lnk_tp == LPFC_LNK_TYPE_GE)
755 			return snprintf(buf, PAGE_SIZE, "fcoe\n");
756 		if (phba->sli4_hba.lnk_info.lnk_tp == LPFC_LNK_TYPE_FC)
757 			return snprintf(buf, PAGE_SIZE, "fc\n");
758 	}
759 	return snprintf(buf, PAGE_SIZE, "unknown\n");
760 }
761 
762 /**
763  * lpfc_oas_supported_show - Return whether or not Optimized Access Storage
764  *			    (OAS) is supported.
765  * @dev: class unused variable.
766  * @attr: device attribute, not used.
767  * @buf: on return contains the module description text.
768  *
769  * Returns: size of formatted string.
770  **/
771 static ssize_t
772 lpfc_oas_supported_show(struct device *dev, struct device_attribute *attr,
773 			char *buf)
774 {
775 	struct Scsi_Host *shost = class_to_shost(dev);
776 	struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
777 	struct lpfc_hba *phba = vport->phba;
778 
779 	return snprintf(buf, PAGE_SIZE, "%d\n",
780 			phba->sli4_hba.pc_sli4_params.oas_supported);
781 }
782 
783 /**
784  * lpfc_link_state_store - Transition the link_state on an HBA port
785  * @dev: class device that is converted into a Scsi_host.
786  * @attr: device attribute, not used.
787  * @buf: one or more lpfc_polling_flags values.
788  * @count: not used.
789  *
790  * Returns:
791  * -EINVAL if the buffer is not "up" or "down"
792  * return from link state change function if non-zero
793  * length of the buf on success
794  **/
795 static ssize_t
796 lpfc_link_state_store(struct device *dev, struct device_attribute *attr,
797 		const char *buf, size_t count)
798 {
799 	struct Scsi_Host  *shost = class_to_shost(dev);
800 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
801 	struct lpfc_hba   *phba = vport->phba;
802 
803 	int status = -EINVAL;
804 
805 	if ((strncmp(buf, "up", sizeof("up") - 1) == 0) &&
806 			(phba->link_state == LPFC_LINK_DOWN))
807 		status = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
808 	else if ((strncmp(buf, "down", sizeof("down") - 1) == 0) &&
809 			(phba->link_state >= LPFC_LINK_UP))
810 		status = phba->lpfc_hba_down_link(phba, MBX_NOWAIT);
811 
812 	if (status == 0)
813 		return strlen(buf);
814 	else
815 		return status;
816 }
817 
818 /**
819  * lpfc_num_discovered_ports_show - Return sum of mapped and unmapped vports
820  * @dev: class device that is converted into a Scsi_host.
821  * @attr: device attribute, not used.
822  * @buf: on return contains the sum of fc mapped and unmapped.
823  *
824  * Description:
825  * Returns the ascii text number of the sum of the fc mapped and unmapped
826  * vport counts.
827  *
828  * Returns: size of formatted string.
829  **/
830 static ssize_t
831 lpfc_num_discovered_ports_show(struct device *dev,
832 			       struct device_attribute *attr, char *buf)
833 {
834 	struct Scsi_Host  *shost = class_to_shost(dev);
835 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
836 
837 	return snprintf(buf, PAGE_SIZE, "%d\n",
838 			vport->fc_map_cnt + vport->fc_unmap_cnt);
839 }
840 
841 /**
842  * lpfc_issue_lip - Misnomer, name carried over from long ago
843  * @shost: Scsi_Host pointer.
844  *
845  * Description:
846  * Bring the link down gracefully then re-init the link. The firmware will
847  * re-init the fiber channel interface as required. Does not issue a LIP.
848  *
849  * Returns:
850  * -EPERM port offline or management commands are being blocked
851  * -ENOMEM cannot allocate memory for the mailbox command
852  * -EIO error sending the mailbox command
853  * zero for success
854  **/
855 static int
856 lpfc_issue_lip(struct Scsi_Host *shost)
857 {
858 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
859 	struct lpfc_hba   *phba = vport->phba;
860 	LPFC_MBOXQ_t *pmboxq;
861 	int mbxstatus = MBXERR_ERROR;
862 
863 	if ((vport->fc_flag & FC_OFFLINE_MODE) ||
864 	    (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO))
865 		return -EPERM;
866 
867 	pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
868 
869 	if (!pmboxq)
870 		return -ENOMEM;
871 
872 	memset((void *)pmboxq, 0, sizeof (LPFC_MBOXQ_t));
873 	pmboxq->u.mb.mbxCommand = MBX_DOWN_LINK;
874 	pmboxq->u.mb.mbxOwner = OWN_HOST;
875 
876 	mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO * 2);
877 
878 	if ((mbxstatus == MBX_SUCCESS) &&
879 	    (pmboxq->u.mb.mbxStatus == 0 ||
880 	     pmboxq->u.mb.mbxStatus == MBXERR_LINK_DOWN)) {
881 		memset((void *)pmboxq, 0, sizeof (LPFC_MBOXQ_t));
882 		lpfc_init_link(phba, pmboxq, phba->cfg_topology,
883 			       phba->cfg_link_speed);
884 		mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
885 						     phba->fc_ratov * 2);
886 		if ((mbxstatus == MBX_SUCCESS) &&
887 		    (pmboxq->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
888 			lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
889 					"2859 SLI authentication is required "
890 					"for INIT_LINK but has not done yet\n");
891 	}
892 
893 	lpfc_set_loopback_flag(phba);
894 	if (mbxstatus != MBX_TIMEOUT)
895 		mempool_free(pmboxq, phba->mbox_mem_pool);
896 
897 	if (mbxstatus == MBXERR_ERROR)
898 		return -EIO;
899 
900 	return 0;
901 }
902 
903 int
904 lpfc_emptyq_wait(struct lpfc_hba *phba, struct list_head *q, spinlock_t *lock)
905 {
906 	int cnt = 0;
907 
908 	spin_lock_irq(lock);
909 	while (!list_empty(q)) {
910 		spin_unlock_irq(lock);
911 		msleep(20);
912 		if (cnt++ > 250) {  /* 5 secs */
913 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
914 					"0466 %s %s\n",
915 					"Outstanding IO when ",
916 					"bringing Adapter offline\n");
917 				return 0;
918 		}
919 		spin_lock_irq(lock);
920 	}
921 	spin_unlock_irq(lock);
922 	return 1;
923 }
924 
925 /**
926  * lpfc_do_offline - Issues a mailbox command to bring the link down
927  * @phba: lpfc_hba pointer.
928  * @type: LPFC_EVT_OFFLINE, LPFC_EVT_WARM_START, LPFC_EVT_KILL.
929  *
930  * Notes:
931  * Assumes any error from lpfc_do_offline() will be negative.
932  * Can wait up to 5 seconds for the port ring buffers count
933  * to reach zero, prints a warning if it is not zero and continues.
934  * lpfc_workq_post_event() returns a non-zero return code if call fails.
935  *
936  * Returns:
937  * -EIO error posting the event
938  * zero for success
939  **/
940 static int
941 lpfc_do_offline(struct lpfc_hba *phba, uint32_t type)
942 {
943 	struct completion online_compl;
944 	struct lpfc_queue *qp = NULL;
945 	struct lpfc_sli_ring *pring;
946 	struct lpfc_sli *psli;
947 	int status = 0;
948 	int i;
949 	int rc;
950 
951 	init_completion(&online_compl);
952 	rc = lpfc_workq_post_event(phba, &status, &online_compl,
953 			      LPFC_EVT_OFFLINE_PREP);
954 	if (rc == 0)
955 		return -ENOMEM;
956 
957 	wait_for_completion(&online_compl);
958 
959 	if (status != 0)
960 		return -EIO;
961 
962 	psli = &phba->sli;
963 
964 	/* Wait a little for things to settle down, but not
965 	 * long enough for dev loss timeout to expire.
966 	 */
967 	if (phba->sli_rev != LPFC_SLI_REV4) {
968 		for (i = 0; i < psli->num_rings; i++) {
969 			pring = &psli->sli3_ring[i];
970 			if (!lpfc_emptyq_wait(phba, &pring->txcmplq,
971 					      &phba->hbalock))
972 				goto out;
973 		}
974 	} else {
975 		list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
976 			pring = qp->pring;
977 			if (!pring)
978 				continue;
979 			if (!lpfc_emptyq_wait(phba, &pring->txcmplq,
980 					      &pring->ring_lock))
981 				goto out;
982 		}
983 	}
984 out:
985 	init_completion(&online_compl);
986 	rc = lpfc_workq_post_event(phba, &status, &online_compl, type);
987 	if (rc == 0)
988 		return -ENOMEM;
989 
990 	wait_for_completion(&online_compl);
991 
992 	if (status != 0)
993 		return -EIO;
994 
995 	return 0;
996 }
997 
998 /**
999  * lpfc_selective_reset - Offline then onlines the port
1000  * @phba: lpfc_hba pointer.
1001  *
1002  * Description:
1003  * If the port is configured to allow a reset then the hba is brought
1004  * offline then online.
1005  *
1006  * Notes:
1007  * Assumes any error from lpfc_do_offline() will be negative.
1008  * Do not make this function static.
1009  *
1010  * Returns:
1011  * lpfc_do_offline() return code if not zero
1012  * -EIO reset not configured or error posting the event
1013  * zero for success
1014  **/
1015 int
1016 lpfc_selective_reset(struct lpfc_hba *phba)
1017 {
1018 	struct completion online_compl;
1019 	int status = 0;
1020 	int rc;
1021 
1022 	if (!phba->cfg_enable_hba_reset)
1023 		return -EACCES;
1024 
1025 	if (!(phba->pport->fc_flag & FC_OFFLINE_MODE)) {
1026 		status = lpfc_do_offline(phba, LPFC_EVT_OFFLINE);
1027 
1028 		if (status != 0)
1029 			return status;
1030 	}
1031 
1032 	init_completion(&online_compl);
1033 	rc = lpfc_workq_post_event(phba, &status, &online_compl,
1034 			      LPFC_EVT_ONLINE);
1035 	if (rc == 0)
1036 		return -ENOMEM;
1037 
1038 	wait_for_completion(&online_compl);
1039 
1040 	if (status != 0)
1041 		return -EIO;
1042 
1043 	return 0;
1044 }
1045 
1046 /**
1047  * lpfc_issue_reset - Selectively resets an adapter
1048  * @dev: class device that is converted into a Scsi_host.
1049  * @attr: device attribute, not used.
1050  * @buf: containing the string "selective".
1051  * @count: unused variable.
1052  *
1053  * Description:
1054  * If the buf contains the string "selective" then lpfc_selective_reset()
1055  * is called to perform the reset.
1056  *
1057  * Notes:
1058  * Assumes any error from lpfc_selective_reset() will be negative.
1059  * If lpfc_selective_reset() returns zero then the length of the buffer
1060  * is returned which indicates success
1061  *
1062  * Returns:
1063  * -EINVAL if the buffer does not contain the string "selective"
1064  * length of buf if lpfc-selective_reset() if the call succeeds
1065  * return value of lpfc_selective_reset() if the call fails
1066 **/
1067 static ssize_t
1068 lpfc_issue_reset(struct device *dev, struct device_attribute *attr,
1069 		 const char *buf, size_t count)
1070 {
1071 	struct Scsi_Host  *shost = class_to_shost(dev);
1072 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1073 	struct lpfc_hba   *phba = vport->phba;
1074 	int status = -EINVAL;
1075 
1076 	if (!phba->cfg_enable_hba_reset)
1077 		return -EACCES;
1078 
1079 	if (strncmp(buf, "selective", sizeof("selective") - 1) == 0)
1080 		status = phba->lpfc_selective_reset(phba);
1081 
1082 	if (status == 0)
1083 		return strlen(buf);
1084 	else
1085 		return status;
1086 }
1087 
1088 /**
1089  * lpfc_sli4_pdev_status_reg_wait - Wait for pdev status register for readyness
1090  * @phba: lpfc_hba pointer.
1091  *
1092  * Description:
1093  * SLI4 interface type-2 device to wait on the sliport status register for
1094  * the readyness after performing a firmware reset.
1095  *
1096  * Returns:
1097  * zero for success, -EPERM when port does not have privilege to perform the
1098  * reset, -EIO when port timeout from recovering from the reset.
1099  *
1100  * Note:
1101  * As the caller will interpret the return code by value, be careful in making
1102  * change or addition to return codes.
1103  **/
1104 int
1105 lpfc_sli4_pdev_status_reg_wait(struct lpfc_hba *phba)
1106 {
1107 	struct lpfc_register portstat_reg = {0};
1108 	int i;
1109 
1110 	msleep(100);
1111 	lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
1112 		   &portstat_reg.word0);
1113 
1114 	/* verify if privileged for the request operation */
1115 	if (!bf_get(lpfc_sliport_status_rn, &portstat_reg) &&
1116 	    !bf_get(lpfc_sliport_status_err, &portstat_reg))
1117 		return -EPERM;
1118 
1119 	/* wait for the SLI port firmware ready after firmware reset */
1120 	for (i = 0; i < LPFC_FW_RESET_MAXIMUM_WAIT_10MS_CNT; i++) {
1121 		msleep(10);
1122 		lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
1123 			   &portstat_reg.word0);
1124 		if (!bf_get(lpfc_sliport_status_err, &portstat_reg))
1125 			continue;
1126 		if (!bf_get(lpfc_sliport_status_rn, &portstat_reg))
1127 			continue;
1128 		if (!bf_get(lpfc_sliport_status_rdy, &portstat_reg))
1129 			continue;
1130 		break;
1131 	}
1132 
1133 	if (i < LPFC_FW_RESET_MAXIMUM_WAIT_10MS_CNT)
1134 		return 0;
1135 	else
1136 		return -EIO;
1137 }
1138 
1139 /**
1140  * lpfc_sli4_pdev_reg_request - Request physical dev to perform a register acc
1141  * @phba: lpfc_hba pointer.
1142  *
1143  * Description:
1144  * Request SLI4 interface type-2 device to perform a physical register set
1145  * access.
1146  *
1147  * Returns:
1148  * zero for success
1149  **/
1150 static ssize_t
1151 lpfc_sli4_pdev_reg_request(struct lpfc_hba *phba, uint32_t opcode)
1152 {
1153 	struct completion online_compl;
1154 	struct pci_dev *pdev = phba->pcidev;
1155 	uint32_t before_fc_flag;
1156 	uint32_t sriov_nr_virtfn;
1157 	uint32_t reg_val;
1158 	int status = 0, rc = 0;
1159 	int job_posted = 1, sriov_err;
1160 
1161 	if (!phba->cfg_enable_hba_reset)
1162 		return -EACCES;
1163 
1164 	if ((phba->sli_rev < LPFC_SLI_REV4) ||
1165 	    (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
1166 	     LPFC_SLI_INTF_IF_TYPE_2))
1167 		return -EPERM;
1168 
1169 	/* Keep state if we need to restore back */
1170 	before_fc_flag = phba->pport->fc_flag;
1171 	sriov_nr_virtfn = phba->cfg_sriov_nr_virtfn;
1172 
1173 	/* Disable SR-IOV virtual functions if enabled */
1174 	if (phba->cfg_sriov_nr_virtfn) {
1175 		pci_disable_sriov(pdev);
1176 		phba->cfg_sriov_nr_virtfn = 0;
1177 	}
1178 
1179 	if (opcode == LPFC_FW_DUMP)
1180 		phba->hba_flag |= HBA_FW_DUMP_OP;
1181 
1182 	status = lpfc_do_offline(phba, LPFC_EVT_OFFLINE);
1183 
1184 	if (status != 0) {
1185 		phba->hba_flag &= ~HBA_FW_DUMP_OP;
1186 		return status;
1187 	}
1188 
1189 	/* wait for the device to be quiesced before firmware reset */
1190 	msleep(100);
1191 
1192 	reg_val = readl(phba->sli4_hba.conf_regs_memmap_p +
1193 			LPFC_CTL_PDEV_CTL_OFFSET);
1194 
1195 	if (opcode == LPFC_FW_DUMP)
1196 		reg_val |= LPFC_FW_DUMP_REQUEST;
1197 	else if (opcode == LPFC_FW_RESET)
1198 		reg_val |= LPFC_CTL_PDEV_CTL_FRST;
1199 	else if (opcode == LPFC_DV_RESET)
1200 		reg_val |= LPFC_CTL_PDEV_CTL_DRST;
1201 
1202 	writel(reg_val, phba->sli4_hba.conf_regs_memmap_p +
1203 	       LPFC_CTL_PDEV_CTL_OFFSET);
1204 	/* flush */
1205 	readl(phba->sli4_hba.conf_regs_memmap_p + LPFC_CTL_PDEV_CTL_OFFSET);
1206 
1207 	/* delay driver action following IF_TYPE_2 reset */
1208 	rc = lpfc_sli4_pdev_status_reg_wait(phba);
1209 
1210 	if (rc == -EPERM) {
1211 		/* no privilege for reset */
1212 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1213 				"3150 No privilege to perform the requested "
1214 				"access: x%x\n", reg_val);
1215 	} else if (rc == -EIO) {
1216 		/* reset failed, there is nothing more we can do */
1217 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1218 				"3153 Fail to perform the requested "
1219 				"access: x%x\n", reg_val);
1220 		return rc;
1221 	}
1222 
1223 	/* keep the original port state */
1224 	if (before_fc_flag & FC_OFFLINE_MODE)
1225 		goto out;
1226 
1227 	init_completion(&online_compl);
1228 	job_posted = lpfc_workq_post_event(phba, &status, &online_compl,
1229 					   LPFC_EVT_ONLINE);
1230 	if (!job_posted)
1231 		goto out;
1232 
1233 	wait_for_completion(&online_compl);
1234 
1235 out:
1236 	/* in any case, restore the virtual functions enabled as before */
1237 	if (sriov_nr_virtfn) {
1238 		sriov_err =
1239 			lpfc_sli_probe_sriov_nr_virtfn(phba, sriov_nr_virtfn);
1240 		if (!sriov_err)
1241 			phba->cfg_sriov_nr_virtfn = sriov_nr_virtfn;
1242 	}
1243 
1244 	/* return proper error code */
1245 	if (!rc) {
1246 		if (!job_posted)
1247 			rc = -ENOMEM;
1248 		else if (status)
1249 			rc = -EIO;
1250 	}
1251 	return rc;
1252 }
1253 
1254 /**
1255  * lpfc_nport_evt_cnt_show - Return the number of nport events
1256  * @dev: class device that is converted into a Scsi_host.
1257  * @attr: device attribute, not used.
1258  * @buf: on return contains the ascii number of nport events.
1259  *
1260  * Returns: size of formatted string.
1261  **/
1262 static ssize_t
1263 lpfc_nport_evt_cnt_show(struct device *dev, struct device_attribute *attr,
1264 			char *buf)
1265 {
1266 	struct Scsi_Host  *shost = class_to_shost(dev);
1267 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1268 	struct lpfc_hba   *phba = vport->phba;
1269 
1270 	return snprintf(buf, PAGE_SIZE, "%d\n", phba->nport_event_cnt);
1271 }
1272 
1273 /**
1274  * lpfc_board_mode_show - Return the state of the board
1275  * @dev: class device that is converted into a Scsi_host.
1276  * @attr: device attribute, not used.
1277  * @buf: on return contains the state of the adapter.
1278  *
1279  * Returns: size of formatted string.
1280  **/
1281 static ssize_t
1282 lpfc_board_mode_show(struct device *dev, struct device_attribute *attr,
1283 		     char *buf)
1284 {
1285 	struct Scsi_Host  *shost = class_to_shost(dev);
1286 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1287 	struct lpfc_hba   *phba = vport->phba;
1288 	char  * state;
1289 
1290 	if (phba->link_state == LPFC_HBA_ERROR)
1291 		state = "error";
1292 	else if (phba->link_state == LPFC_WARM_START)
1293 		state = "warm start";
1294 	else if (phba->link_state == LPFC_INIT_START)
1295 		state = "offline";
1296 	else
1297 		state = "online";
1298 
1299 	return snprintf(buf, PAGE_SIZE, "%s\n", state);
1300 }
1301 
1302 /**
1303  * lpfc_board_mode_store - Puts the hba in online, offline, warm or error state
1304  * @dev: class device that is converted into a Scsi_host.
1305  * @attr: device attribute, not used.
1306  * @buf: containing one of the strings "online", "offline", "warm" or "error".
1307  * @count: unused variable.
1308  *
1309  * Returns:
1310  * -EACCES if enable hba reset not enabled
1311  * -EINVAL if the buffer does not contain a valid string (see above)
1312  * -EIO if lpfc_workq_post_event() or lpfc_do_offline() fails
1313  * buf length greater than zero indicates success
1314  **/
1315 static ssize_t
1316 lpfc_board_mode_store(struct device *dev, struct device_attribute *attr,
1317 		      const char *buf, size_t count)
1318 {
1319 	struct Scsi_Host  *shost = class_to_shost(dev);
1320 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1321 	struct lpfc_hba   *phba = vport->phba;
1322 	struct completion online_compl;
1323 	char *board_mode_str = NULL;
1324 	int status = 0;
1325 	int rc;
1326 
1327 	if (!phba->cfg_enable_hba_reset) {
1328 		status = -EACCES;
1329 		goto board_mode_out;
1330 	}
1331 
1332 	lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
1333 			 "3050 lpfc_board_mode set to %s\n", buf);
1334 
1335 	init_completion(&online_compl);
1336 
1337 	if(strncmp(buf, "online", sizeof("online") - 1) == 0) {
1338 		rc = lpfc_workq_post_event(phba, &status, &online_compl,
1339 				      LPFC_EVT_ONLINE);
1340 		if (rc == 0) {
1341 			status = -ENOMEM;
1342 			goto board_mode_out;
1343 		}
1344 		wait_for_completion(&online_compl);
1345 	} else if (strncmp(buf, "offline", sizeof("offline") - 1) == 0)
1346 		status = lpfc_do_offline(phba, LPFC_EVT_OFFLINE);
1347 	else if (strncmp(buf, "warm", sizeof("warm") - 1) == 0)
1348 		if (phba->sli_rev == LPFC_SLI_REV4)
1349 			status = -EINVAL;
1350 		else
1351 			status = lpfc_do_offline(phba, LPFC_EVT_WARM_START);
1352 	else if (strncmp(buf, "error", sizeof("error") - 1) == 0)
1353 		if (phba->sli_rev == LPFC_SLI_REV4)
1354 			status = -EINVAL;
1355 		else
1356 			status = lpfc_do_offline(phba, LPFC_EVT_KILL);
1357 	else if (strncmp(buf, "dump", sizeof("dump") - 1) == 0)
1358 		status = lpfc_sli4_pdev_reg_request(phba, LPFC_FW_DUMP);
1359 	else if (strncmp(buf, "fw_reset", sizeof("fw_reset") - 1) == 0)
1360 		status = lpfc_sli4_pdev_reg_request(phba, LPFC_FW_RESET);
1361 	else if (strncmp(buf, "dv_reset", sizeof("dv_reset") - 1) == 0)
1362 		status = lpfc_sli4_pdev_reg_request(phba, LPFC_DV_RESET);
1363 	else
1364 		status = -EINVAL;
1365 
1366 board_mode_out:
1367 	if (!status)
1368 		return strlen(buf);
1369 	else {
1370 		board_mode_str = strchr(buf, '\n');
1371 		if (board_mode_str)
1372 			*board_mode_str = '\0';
1373 		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
1374 				 "3097 Failed \"%s\", status(%d), "
1375 				 "fc_flag(x%x)\n",
1376 				 buf, status, phba->pport->fc_flag);
1377 		return status;
1378 	}
1379 }
1380 
1381 /**
1382  * lpfc_get_hba_info - Return various bits of informaton about the adapter
1383  * @phba: pointer to the adapter structure.
1384  * @mxri: max xri count.
1385  * @axri: available xri count.
1386  * @mrpi: max rpi count.
1387  * @arpi: available rpi count.
1388  * @mvpi: max vpi count.
1389  * @avpi: available vpi count.
1390  *
1391  * Description:
1392  * If an integer pointer for an count is not null then the value for the
1393  * count is returned.
1394  *
1395  * Returns:
1396  * zero on error
1397  * one for success
1398  **/
1399 static int
1400 lpfc_get_hba_info(struct lpfc_hba *phba,
1401 		  uint32_t *mxri, uint32_t *axri,
1402 		  uint32_t *mrpi, uint32_t *arpi,
1403 		  uint32_t *mvpi, uint32_t *avpi)
1404 {
1405 	struct lpfc_mbx_read_config *rd_config;
1406 	LPFC_MBOXQ_t *pmboxq;
1407 	MAILBOX_t *pmb;
1408 	int rc = 0;
1409 	uint32_t max_vpi;
1410 
1411 	/*
1412 	 * prevent udev from issuing mailbox commands until the port is
1413 	 * configured.
1414 	 */
1415 	if (phba->link_state < LPFC_LINK_DOWN ||
1416 	    !phba->mbox_mem_pool ||
1417 	    (phba->sli.sli_flag & LPFC_SLI_ACTIVE) == 0)
1418 		return 0;
1419 
1420 	if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO)
1421 		return 0;
1422 
1423 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1424 	if (!pmboxq)
1425 		return 0;
1426 	memset(pmboxq, 0, sizeof (LPFC_MBOXQ_t));
1427 
1428 	pmb = &pmboxq->u.mb;
1429 	pmb->mbxCommand = MBX_READ_CONFIG;
1430 	pmb->mbxOwner = OWN_HOST;
1431 	pmboxq->context1 = NULL;
1432 
1433 	if (phba->pport->fc_flag & FC_OFFLINE_MODE)
1434 		rc = MBX_NOT_FINISHED;
1435 	else
1436 		rc = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
1437 
1438 	if (rc != MBX_SUCCESS) {
1439 		if (rc != MBX_TIMEOUT)
1440 			mempool_free(pmboxq, phba->mbox_mem_pool);
1441 		return 0;
1442 	}
1443 
1444 	if (phba->sli_rev == LPFC_SLI_REV4) {
1445 		rd_config = &pmboxq->u.mqe.un.rd_config;
1446 		if (mrpi)
1447 			*mrpi = bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
1448 		if (arpi)
1449 			*arpi = bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config) -
1450 					phba->sli4_hba.max_cfg_param.rpi_used;
1451 		if (mxri)
1452 			*mxri = bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
1453 		if (axri)
1454 			*axri = bf_get(lpfc_mbx_rd_conf_xri_count, rd_config) -
1455 					phba->sli4_hba.max_cfg_param.xri_used;
1456 
1457 		/* Account for differences with SLI-3.  Get vpi count from
1458 		 * mailbox data and subtract one for max vpi value.
1459 		 */
1460 		max_vpi = (bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config) > 0) ?
1461 			(bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config) - 1) : 0;
1462 
1463 		if (mvpi)
1464 			*mvpi = max_vpi;
1465 		if (avpi)
1466 			*avpi = max_vpi - phba->sli4_hba.max_cfg_param.vpi_used;
1467 	} else {
1468 		if (mrpi)
1469 			*mrpi = pmb->un.varRdConfig.max_rpi;
1470 		if (arpi)
1471 			*arpi = pmb->un.varRdConfig.avail_rpi;
1472 		if (mxri)
1473 			*mxri = pmb->un.varRdConfig.max_xri;
1474 		if (axri)
1475 			*axri = pmb->un.varRdConfig.avail_xri;
1476 		if (mvpi)
1477 			*mvpi = pmb->un.varRdConfig.max_vpi;
1478 		if (avpi)
1479 			*avpi = pmb->un.varRdConfig.avail_vpi;
1480 	}
1481 
1482 	mempool_free(pmboxq, phba->mbox_mem_pool);
1483 	return 1;
1484 }
1485 
1486 /**
1487  * lpfc_max_rpi_show - Return maximum rpi
1488  * @dev: class device that is converted into a Scsi_host.
1489  * @attr: device attribute, not used.
1490  * @buf: on return contains the maximum rpi count in decimal or "Unknown".
1491  *
1492  * Description:
1493  * Calls lpfc_get_hba_info() asking for just the mrpi count.
1494  * If lpfc_get_hba_info() returns zero (failure) the buffer text is set
1495  * to "Unknown" and the buffer length is returned, therefore the caller
1496  * must check for "Unknown" in the buffer to detect a failure.
1497  *
1498  * Returns: size of formatted string.
1499  **/
1500 static ssize_t
1501 lpfc_max_rpi_show(struct device *dev, struct device_attribute *attr,
1502 		  char *buf)
1503 {
1504 	struct Scsi_Host  *shost = class_to_shost(dev);
1505 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1506 	struct lpfc_hba   *phba = vport->phba;
1507 	uint32_t cnt;
1508 
1509 	if (lpfc_get_hba_info(phba, NULL, NULL, &cnt, NULL, NULL, NULL))
1510 		return snprintf(buf, PAGE_SIZE, "%d\n", cnt);
1511 	return snprintf(buf, PAGE_SIZE, "Unknown\n");
1512 }
1513 
1514 /**
1515  * lpfc_used_rpi_show - Return maximum rpi minus available rpi
1516  * @dev: class device that is converted into a Scsi_host.
1517  * @attr: device attribute, not used.
1518  * @buf: containing the used rpi count in decimal or "Unknown".
1519  *
1520  * Description:
1521  * Calls lpfc_get_hba_info() asking for just the mrpi and arpi counts.
1522  * If lpfc_get_hba_info() returns zero (failure) the buffer text is set
1523  * to "Unknown" and the buffer length is returned, therefore the caller
1524  * must check for "Unknown" in the buffer to detect a failure.
1525  *
1526  * Returns: size of formatted string.
1527  **/
1528 static ssize_t
1529 lpfc_used_rpi_show(struct device *dev, struct device_attribute *attr,
1530 		   char *buf)
1531 {
1532 	struct Scsi_Host  *shost = class_to_shost(dev);
1533 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1534 	struct lpfc_hba   *phba = vport->phba;
1535 	uint32_t cnt, acnt;
1536 
1537 	if (lpfc_get_hba_info(phba, NULL, NULL, &cnt, &acnt, NULL, NULL))
1538 		return snprintf(buf, PAGE_SIZE, "%d\n", (cnt - acnt));
1539 	return snprintf(buf, PAGE_SIZE, "Unknown\n");
1540 }
1541 
1542 /**
1543  * lpfc_max_xri_show - Return maximum xri
1544  * @dev: class device that is converted into a Scsi_host.
1545  * @attr: device attribute, not used.
1546  * @buf: on return contains the maximum xri count in decimal or "Unknown".
1547  *
1548  * Description:
1549  * Calls lpfc_get_hba_info() asking for just the mrpi count.
1550  * If lpfc_get_hba_info() returns zero (failure) the buffer text is set
1551  * to "Unknown" and the buffer length is returned, therefore the caller
1552  * must check for "Unknown" in the buffer to detect a failure.
1553  *
1554  * Returns: size of formatted string.
1555  **/
1556 static ssize_t
1557 lpfc_max_xri_show(struct device *dev, struct device_attribute *attr,
1558 		  char *buf)
1559 {
1560 	struct Scsi_Host  *shost = class_to_shost(dev);
1561 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1562 	struct lpfc_hba   *phba = vport->phba;
1563 	uint32_t cnt;
1564 
1565 	if (lpfc_get_hba_info(phba, &cnt, NULL, NULL, NULL, NULL, NULL))
1566 		return snprintf(buf, PAGE_SIZE, "%d\n", cnt);
1567 	return snprintf(buf, PAGE_SIZE, "Unknown\n");
1568 }
1569 
1570 /**
1571  * lpfc_used_xri_show - Return maximum xpi minus the available xpi
1572  * @dev: class device that is converted into a Scsi_host.
1573  * @attr: device attribute, not used.
1574  * @buf: on return contains the used xri count in decimal or "Unknown".
1575  *
1576  * Description:
1577  * Calls lpfc_get_hba_info() asking for just the mxri and axri counts.
1578  * If lpfc_get_hba_info() returns zero (failure) the buffer text is set
1579  * to "Unknown" and the buffer length is returned, therefore the caller
1580  * must check for "Unknown" in the buffer to detect a failure.
1581  *
1582  * Returns: size of formatted string.
1583  **/
1584 static ssize_t
1585 lpfc_used_xri_show(struct device *dev, struct device_attribute *attr,
1586 		   char *buf)
1587 {
1588 	struct Scsi_Host  *shost = class_to_shost(dev);
1589 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1590 	struct lpfc_hba   *phba = vport->phba;
1591 	uint32_t cnt, acnt;
1592 
1593 	if (lpfc_get_hba_info(phba, &cnt, &acnt, NULL, NULL, NULL, NULL))
1594 		return snprintf(buf, PAGE_SIZE, "%d\n", (cnt - acnt));
1595 	return snprintf(buf, PAGE_SIZE, "Unknown\n");
1596 }
1597 
1598 /**
1599  * lpfc_max_vpi_show - Return maximum vpi
1600  * @dev: class device that is converted into a Scsi_host.
1601  * @attr: device attribute, not used.
1602  * @buf: on return contains the maximum vpi count in decimal or "Unknown".
1603  *
1604  * Description:
1605  * Calls lpfc_get_hba_info() asking for just the mvpi count.
1606  * If lpfc_get_hba_info() returns zero (failure) the buffer text is set
1607  * to "Unknown" and the buffer length is returned, therefore the caller
1608  * must check for "Unknown" in the buffer to detect a failure.
1609  *
1610  * Returns: size of formatted string.
1611  **/
1612 static ssize_t
1613 lpfc_max_vpi_show(struct device *dev, struct device_attribute *attr,
1614 		  char *buf)
1615 {
1616 	struct Scsi_Host  *shost = class_to_shost(dev);
1617 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1618 	struct lpfc_hba   *phba = vport->phba;
1619 	uint32_t cnt;
1620 
1621 	if (lpfc_get_hba_info(phba, NULL, NULL, NULL, NULL, &cnt, NULL))
1622 		return snprintf(buf, PAGE_SIZE, "%d\n", cnt);
1623 	return snprintf(buf, PAGE_SIZE, "Unknown\n");
1624 }
1625 
1626 /**
1627  * lpfc_used_vpi_show - Return maximum vpi minus the available vpi
1628  * @dev: class device that is converted into a Scsi_host.
1629  * @attr: device attribute, not used.
1630  * @buf: on return contains the used vpi count in decimal or "Unknown".
1631  *
1632  * Description:
1633  * Calls lpfc_get_hba_info() asking for just the mvpi and avpi counts.
1634  * If lpfc_get_hba_info() returns zero (failure) the buffer text is set
1635  * to "Unknown" and the buffer length is returned, therefore the caller
1636  * must check for "Unknown" in the buffer to detect a failure.
1637  *
1638  * Returns: size of formatted string.
1639  **/
1640 static ssize_t
1641 lpfc_used_vpi_show(struct device *dev, struct device_attribute *attr,
1642 		   char *buf)
1643 {
1644 	struct Scsi_Host  *shost = class_to_shost(dev);
1645 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1646 	struct lpfc_hba   *phba = vport->phba;
1647 	uint32_t cnt, acnt;
1648 
1649 	if (lpfc_get_hba_info(phba, NULL, NULL, NULL, NULL, &cnt, &acnt))
1650 		return snprintf(buf, PAGE_SIZE, "%d\n", (cnt - acnt));
1651 	return snprintf(buf, PAGE_SIZE, "Unknown\n");
1652 }
1653 
1654 /**
1655  * lpfc_npiv_info_show - Return text about NPIV support for the adapter
1656  * @dev: class device that is converted into a Scsi_host.
1657  * @attr: device attribute, not used.
1658  * @buf: text that must be interpreted to determine if npiv is supported.
1659  *
1660  * Description:
1661  * Buffer will contain text indicating npiv is not suppoerted on the port,
1662  * the port is an NPIV physical port, or it is an npiv virtual port with
1663  * the id of the vport.
1664  *
1665  * Returns: size of formatted string.
1666  **/
1667 static ssize_t
1668 lpfc_npiv_info_show(struct device *dev, struct device_attribute *attr,
1669 		    char *buf)
1670 {
1671 	struct Scsi_Host  *shost = class_to_shost(dev);
1672 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1673 	struct lpfc_hba   *phba = vport->phba;
1674 
1675 	if (!(phba->max_vpi))
1676 		return snprintf(buf, PAGE_SIZE, "NPIV Not Supported\n");
1677 	if (vport->port_type == LPFC_PHYSICAL_PORT)
1678 		return snprintf(buf, PAGE_SIZE, "NPIV Physical\n");
1679 	return snprintf(buf, PAGE_SIZE, "NPIV Virtual (VPI %d)\n", vport->vpi);
1680 }
1681 
1682 /**
1683  * lpfc_poll_show - Return text about poll support for the adapter
1684  * @dev: class device that is converted into a Scsi_host.
1685  * @attr: device attribute, not used.
1686  * @buf: on return contains the cfg_poll in hex.
1687  *
1688  * Notes:
1689  * cfg_poll should be a lpfc_polling_flags type.
1690  *
1691  * Returns: size of formatted string.
1692  **/
1693 static ssize_t
1694 lpfc_poll_show(struct device *dev, struct device_attribute *attr,
1695 	       char *buf)
1696 {
1697 	struct Scsi_Host  *shost = class_to_shost(dev);
1698 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1699 	struct lpfc_hba   *phba = vport->phba;
1700 
1701 	return snprintf(buf, PAGE_SIZE, "%#x\n", phba->cfg_poll);
1702 }
1703 
1704 /**
1705  * lpfc_poll_store - Set the value of cfg_poll for the adapter
1706  * @dev: class device that is converted into a Scsi_host.
1707  * @attr: device attribute, not used.
1708  * @buf: one or more lpfc_polling_flags values.
1709  * @count: not used.
1710  *
1711  * Notes:
1712  * buf contents converted to integer and checked for a valid value.
1713  *
1714  * Returns:
1715  * -EINVAL if the buffer connot be converted or is out of range
1716  * length of the buf on success
1717  **/
1718 static ssize_t
1719 lpfc_poll_store(struct device *dev, struct device_attribute *attr,
1720 		const char *buf, size_t count)
1721 {
1722 	struct Scsi_Host  *shost = class_to_shost(dev);
1723 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1724 	struct lpfc_hba   *phba = vport->phba;
1725 	uint32_t creg_val;
1726 	uint32_t old_val;
1727 	int val=0;
1728 
1729 	if (!isdigit(buf[0]))
1730 		return -EINVAL;
1731 
1732 	if (sscanf(buf, "%i", &val) != 1)
1733 		return -EINVAL;
1734 
1735 	if ((val & 0x3) != val)
1736 		return -EINVAL;
1737 
1738 	if (phba->sli_rev == LPFC_SLI_REV4)
1739 		val = 0;
1740 
1741 	lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
1742 		"3051 lpfc_poll changed from %d to %d\n",
1743 		phba->cfg_poll, val);
1744 
1745 	spin_lock_irq(&phba->hbalock);
1746 
1747 	old_val = phba->cfg_poll;
1748 
1749 	if (val & ENABLE_FCP_RING_POLLING) {
1750 		if ((val & DISABLE_FCP_RING_INT) &&
1751 		    !(old_val & DISABLE_FCP_RING_INT)) {
1752 			if (lpfc_readl(phba->HCregaddr, &creg_val)) {
1753 				spin_unlock_irq(&phba->hbalock);
1754 				return -EINVAL;
1755 			}
1756 			creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
1757 			writel(creg_val, phba->HCregaddr);
1758 			readl(phba->HCregaddr); /* flush */
1759 
1760 			lpfc_poll_start_timer(phba);
1761 		}
1762 	} else if (val != 0x0) {
1763 		spin_unlock_irq(&phba->hbalock);
1764 		return -EINVAL;
1765 	}
1766 
1767 	if (!(val & DISABLE_FCP_RING_INT) &&
1768 	    (old_val & DISABLE_FCP_RING_INT))
1769 	{
1770 		spin_unlock_irq(&phba->hbalock);
1771 		del_timer(&phba->fcp_poll_timer);
1772 		spin_lock_irq(&phba->hbalock);
1773 		if (lpfc_readl(phba->HCregaddr, &creg_val)) {
1774 			spin_unlock_irq(&phba->hbalock);
1775 			return -EINVAL;
1776 		}
1777 		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
1778 		writel(creg_val, phba->HCregaddr);
1779 		readl(phba->HCregaddr); /* flush */
1780 	}
1781 
1782 	phba->cfg_poll = val;
1783 
1784 	spin_unlock_irq(&phba->hbalock);
1785 
1786 	return strlen(buf);
1787 }
1788 
1789 /**
1790  * lpfc_fips_level_show - Return the current FIPS level for the HBA
1791  * @dev: class unused variable.
1792  * @attr: device attribute, not used.
1793  * @buf: on return contains the module description text.
1794  *
1795  * Returns: size of formatted string.
1796  **/
1797 static ssize_t
1798 lpfc_fips_level_show(struct device *dev,  struct device_attribute *attr,
1799 		     char *buf)
1800 {
1801 	struct Scsi_Host  *shost = class_to_shost(dev);
1802 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1803 	struct lpfc_hba   *phba = vport->phba;
1804 
1805 	return snprintf(buf, PAGE_SIZE, "%d\n", phba->fips_level);
1806 }
1807 
1808 /**
1809  * lpfc_fips_rev_show - Return the FIPS Spec revision for the HBA
1810  * @dev: class unused variable.
1811  * @attr: device attribute, not used.
1812  * @buf: on return contains the module description text.
1813  *
1814  * Returns: size of formatted string.
1815  **/
1816 static ssize_t
1817 lpfc_fips_rev_show(struct device *dev,  struct device_attribute *attr,
1818 		   char *buf)
1819 {
1820 	struct Scsi_Host  *shost = class_to_shost(dev);
1821 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1822 	struct lpfc_hba   *phba = vport->phba;
1823 
1824 	return snprintf(buf, PAGE_SIZE, "%d\n", phba->fips_spec_rev);
1825 }
1826 
1827 /**
1828  * lpfc_dss_show - Return the current state of dss and the configured state
1829  * @dev: class converted to a Scsi_host structure.
1830  * @attr: device attribute, not used.
1831  * @buf: on return contains the formatted text.
1832  *
1833  * Returns: size of formatted string.
1834  **/
1835 static ssize_t
1836 lpfc_dss_show(struct device *dev, struct device_attribute *attr,
1837 	      char *buf)
1838 {
1839 	struct Scsi_Host *shost = class_to_shost(dev);
1840 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1841 	struct lpfc_hba   *phba = vport->phba;
1842 
1843 	return snprintf(buf, PAGE_SIZE, "%s - %sOperational\n",
1844 			(phba->cfg_enable_dss) ? "Enabled" : "Disabled",
1845 			(phba->sli3_options & LPFC_SLI3_DSS_ENABLED) ?
1846 				"" : "Not ");
1847 }
1848 
1849 /**
1850  * lpfc_sriov_hw_max_virtfn_show - Return maximum number of virtual functions
1851  * @dev: class converted to a Scsi_host structure.
1852  * @attr: device attribute, not used.
1853  * @buf: on return contains the formatted support level.
1854  *
1855  * Description:
1856  * Returns the maximum number of virtual functions a physical function can
1857  * support, 0 will be returned if called on virtual function.
1858  *
1859  * Returns: size of formatted string.
1860  **/
1861 static ssize_t
1862 lpfc_sriov_hw_max_virtfn_show(struct device *dev,
1863 			      struct device_attribute *attr,
1864 			      char *buf)
1865 {
1866 	struct Scsi_Host *shost = class_to_shost(dev);
1867 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1868 	struct lpfc_hba *phba = vport->phba;
1869 	uint16_t max_nr_virtfn;
1870 
1871 	max_nr_virtfn = lpfc_sli_sriov_nr_virtfn_get(phba);
1872 	return snprintf(buf, PAGE_SIZE, "%d\n", max_nr_virtfn);
1873 }
1874 
1875 static inline bool lpfc_rangecheck(uint val, uint min, uint max)
1876 {
1877 	return val >= min && val <= max;
1878 }
1879 
1880 /**
1881  * lpfc_param_show - Return a cfg attribute value in decimal
1882  *
1883  * Description:
1884  * Macro that given an attr e.g. hba_queue_depth expands
1885  * into a function with the name lpfc_hba_queue_depth_show.
1886  *
1887  * lpfc_##attr##_show: Return the decimal value of an adapters cfg_xxx field.
1888  * @dev: class device that is converted into a Scsi_host.
1889  * @attr: device attribute, not used.
1890  * @buf: on return contains the attribute value in decimal.
1891  *
1892  * Returns: size of formatted string.
1893  **/
1894 #define lpfc_param_show(attr)	\
1895 static ssize_t \
1896 lpfc_##attr##_show(struct device *dev, struct device_attribute *attr, \
1897 		   char *buf) \
1898 { \
1899 	struct Scsi_Host  *shost = class_to_shost(dev);\
1900 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;\
1901 	struct lpfc_hba   *phba = vport->phba;\
1902 	return snprintf(buf, PAGE_SIZE, "%d\n",\
1903 			phba->cfg_##attr);\
1904 }
1905 
1906 /**
1907  * lpfc_param_hex_show - Return a cfg attribute value in hex
1908  *
1909  * Description:
1910  * Macro that given an attr e.g. hba_queue_depth expands
1911  * into a function with the name lpfc_hba_queue_depth_show
1912  *
1913  * lpfc_##attr##_show: Return the hex value of an adapters cfg_xxx field.
1914  * @dev: class device that is converted into a Scsi_host.
1915  * @attr: device attribute, not used.
1916  * @buf: on return contains the attribute value in hexadecimal.
1917  *
1918  * Returns: size of formatted string.
1919  **/
1920 #define lpfc_param_hex_show(attr)	\
1921 static ssize_t \
1922 lpfc_##attr##_show(struct device *dev, struct device_attribute *attr, \
1923 		   char *buf) \
1924 { \
1925 	struct Scsi_Host  *shost = class_to_shost(dev);\
1926 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;\
1927 	struct lpfc_hba   *phba = vport->phba;\
1928 	uint val = 0;\
1929 	val = phba->cfg_##attr;\
1930 	return snprintf(buf, PAGE_SIZE, "%#x\n",\
1931 			phba->cfg_##attr);\
1932 }
1933 
1934 /**
1935  * lpfc_param_init - Initializes a cfg attribute
1936  *
1937  * Description:
1938  * Macro that given an attr e.g. hba_queue_depth expands
1939  * into a function with the name lpfc_hba_queue_depth_init. The macro also
1940  * takes a default argument, a minimum and maximum argument.
1941  *
1942  * lpfc_##attr##_init: Initializes an attribute.
1943  * @phba: pointer the the adapter structure.
1944  * @val: integer attribute value.
1945  *
1946  * Validates the min and max values then sets the adapter config field
1947  * accordingly, or uses the default if out of range and prints an error message.
1948  *
1949  * Returns:
1950  * zero on success
1951  * -EINVAL if default used
1952  **/
1953 #define lpfc_param_init(attr, default, minval, maxval)	\
1954 static int \
1955 lpfc_##attr##_init(struct lpfc_hba *phba, uint val) \
1956 { \
1957 	if (lpfc_rangecheck(val, minval, maxval)) {\
1958 		phba->cfg_##attr = val;\
1959 		return 0;\
1960 	}\
1961 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT, \
1962 			"0449 lpfc_"#attr" attribute cannot be set to %d, "\
1963 			"allowed range is ["#minval", "#maxval"]\n", val); \
1964 	phba->cfg_##attr = default;\
1965 	return -EINVAL;\
1966 }
1967 
1968 /**
1969  * lpfc_param_set - Set a cfg attribute value
1970  *
1971  * Description:
1972  * Macro that given an attr e.g. hba_queue_depth expands
1973  * into a function with the name lpfc_hba_queue_depth_set
1974  *
1975  * lpfc_##attr##_set: Sets an attribute value.
1976  * @phba: pointer the the adapter structure.
1977  * @val: integer attribute value.
1978  *
1979  * Description:
1980  * Validates the min and max values then sets the
1981  * adapter config field if in the valid range. prints error message
1982  * and does not set the parameter if invalid.
1983  *
1984  * Returns:
1985  * zero on success
1986  * -EINVAL if val is invalid
1987  **/
1988 #define lpfc_param_set(attr, default, minval, maxval)	\
1989 static int \
1990 lpfc_##attr##_set(struct lpfc_hba *phba, uint val) \
1991 { \
1992 	if (lpfc_rangecheck(val, minval, maxval)) {\
1993 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT, \
1994 			"3052 lpfc_" #attr " changed from %d to %d\n", \
1995 			phba->cfg_##attr, val); \
1996 		phba->cfg_##attr = val;\
1997 		return 0;\
1998 	}\
1999 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT, \
2000 			"0450 lpfc_"#attr" attribute cannot be set to %d, "\
2001 			"allowed range is ["#minval", "#maxval"]\n", val); \
2002 	return -EINVAL;\
2003 }
2004 
2005 /**
2006  * lpfc_param_store - Set a vport attribute value
2007  *
2008  * Description:
2009  * Macro that given an attr e.g. hba_queue_depth expands
2010  * into a function with the name lpfc_hba_queue_depth_store.
2011  *
2012  * lpfc_##attr##_store: Set an sttribute value.
2013  * @dev: class device that is converted into a Scsi_host.
2014  * @attr: device attribute, not used.
2015  * @buf: contains the attribute value in ascii.
2016  * @count: not used.
2017  *
2018  * Description:
2019  * Convert the ascii text number to an integer, then
2020  * use the lpfc_##attr##_set function to set the value.
2021  *
2022  * Returns:
2023  * -EINVAL if val is invalid or lpfc_##attr##_set() fails
2024  * length of buffer upon success.
2025  **/
2026 #define lpfc_param_store(attr)	\
2027 static ssize_t \
2028 lpfc_##attr##_store(struct device *dev, struct device_attribute *attr, \
2029 		    const char *buf, size_t count) \
2030 { \
2031 	struct Scsi_Host  *shost = class_to_shost(dev);\
2032 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;\
2033 	struct lpfc_hba   *phba = vport->phba;\
2034 	uint val = 0;\
2035 	if (!isdigit(buf[0]))\
2036 		return -EINVAL;\
2037 	if (sscanf(buf, "%i", &val) != 1)\
2038 		return -EINVAL;\
2039 	if (lpfc_##attr##_set(phba, val) == 0) \
2040 		return strlen(buf);\
2041 	else \
2042 		return -EINVAL;\
2043 }
2044 
2045 /**
2046  * lpfc_vport_param_show - Return decimal formatted cfg attribute value
2047  *
2048  * Description:
2049  * Macro that given an attr e.g. hba_queue_depth expands
2050  * into a function with the name lpfc_hba_queue_depth_show
2051  *
2052  * lpfc_##attr##_show: prints the attribute value in decimal.
2053  * @dev: class device that is converted into a Scsi_host.
2054  * @attr: device attribute, not used.
2055  * @buf: on return contains the attribute value in decimal.
2056  *
2057  * Returns: length of formatted string.
2058  **/
2059 #define lpfc_vport_param_show(attr)	\
2060 static ssize_t \
2061 lpfc_##attr##_show(struct device *dev, struct device_attribute *attr, \
2062 		   char *buf) \
2063 { \
2064 	struct Scsi_Host  *shost = class_to_shost(dev);\
2065 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;\
2066 	return snprintf(buf, PAGE_SIZE, "%d\n", vport->cfg_##attr);\
2067 }
2068 
2069 /**
2070  * lpfc_vport_param_hex_show - Return hex formatted attribute value
2071  *
2072  * Description:
2073  * Macro that given an attr e.g.
2074  * hba_queue_depth expands into a function with the name
2075  * lpfc_hba_queue_depth_show
2076  *
2077  * lpfc_##attr##_show: prints the attribute value in hexadecimal.
2078  * @dev: class device that is converted into a Scsi_host.
2079  * @attr: device attribute, not used.
2080  * @buf: on return contains the attribute value in hexadecimal.
2081  *
2082  * Returns: length of formatted string.
2083  **/
2084 #define lpfc_vport_param_hex_show(attr)	\
2085 static ssize_t \
2086 lpfc_##attr##_show(struct device *dev, struct device_attribute *attr, \
2087 		   char *buf) \
2088 { \
2089 	struct Scsi_Host  *shost = class_to_shost(dev);\
2090 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;\
2091 	return snprintf(buf, PAGE_SIZE, "%#x\n", vport->cfg_##attr);\
2092 }
2093 
2094 /**
2095  * lpfc_vport_param_init - Initialize a vport cfg attribute
2096  *
2097  * Description:
2098  * Macro that given an attr e.g. hba_queue_depth expands
2099  * into a function with the name lpfc_hba_queue_depth_init. The macro also
2100  * takes a default argument, a minimum and maximum argument.
2101  *
2102  * lpfc_##attr##_init: validates the min and max values then sets the
2103  * adapter config field accordingly, or uses the default if out of range
2104  * and prints an error message.
2105  * @phba: pointer the the adapter structure.
2106  * @val: integer attribute value.
2107  *
2108  * Returns:
2109  * zero on success
2110  * -EINVAL if default used
2111  **/
2112 #define lpfc_vport_param_init(attr, default, minval, maxval)	\
2113 static int \
2114 lpfc_##attr##_init(struct lpfc_vport *vport, uint val) \
2115 { \
2116 	if (lpfc_rangecheck(val, minval, maxval)) {\
2117 		vport->cfg_##attr = val;\
2118 		return 0;\
2119 	}\
2120 	lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT, \
2121 			 "0423 lpfc_"#attr" attribute cannot be set to %d, "\
2122 			 "allowed range is ["#minval", "#maxval"]\n", val); \
2123 	vport->cfg_##attr = default;\
2124 	return -EINVAL;\
2125 }
2126 
2127 /**
2128  * lpfc_vport_param_set - Set a vport cfg attribute
2129  *
2130  * Description:
2131  * Macro that given an attr e.g. hba_queue_depth expands
2132  * into a function with the name lpfc_hba_queue_depth_set
2133  *
2134  * lpfc_##attr##_set: validates the min and max values then sets the
2135  * adapter config field if in the valid range. prints error message
2136  * and does not set the parameter if invalid.
2137  * @phba: pointer the the adapter structure.
2138  * @val:	integer attribute value.
2139  *
2140  * Returns:
2141  * zero on success
2142  * -EINVAL if val is invalid
2143  **/
2144 #define lpfc_vport_param_set(attr, default, minval, maxval)	\
2145 static int \
2146 lpfc_##attr##_set(struct lpfc_vport *vport, uint val) \
2147 { \
2148 	if (lpfc_rangecheck(val, minval, maxval)) {\
2149 		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT, \
2150 			"3053 lpfc_" #attr \
2151 			" changed from %d (x%x) to %d (x%x)\n", \
2152 			vport->cfg_##attr, vport->cfg_##attr, \
2153 			val, val); \
2154 		vport->cfg_##attr = val;\
2155 		return 0;\
2156 	}\
2157 	lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT, \
2158 			 "0424 lpfc_"#attr" attribute cannot be set to %d, "\
2159 			 "allowed range is ["#minval", "#maxval"]\n", val); \
2160 	return -EINVAL;\
2161 }
2162 
2163 /**
2164  * lpfc_vport_param_store - Set a vport attribute
2165  *
2166  * Description:
2167  * Macro that given an attr e.g. hba_queue_depth
2168  * expands into a function with the name lpfc_hba_queue_depth_store
2169  *
2170  * lpfc_##attr##_store: convert the ascii text number to an integer, then
2171  * use the lpfc_##attr##_set function to set the value.
2172  * @cdev: class device that is converted into a Scsi_host.
2173  * @buf:	contains the attribute value in decimal.
2174  * @count: not used.
2175  *
2176  * Returns:
2177  * -EINVAL if val is invalid or lpfc_##attr##_set() fails
2178  * length of buffer upon success.
2179  **/
2180 #define lpfc_vport_param_store(attr)	\
2181 static ssize_t \
2182 lpfc_##attr##_store(struct device *dev, struct device_attribute *attr, \
2183 		    const char *buf, size_t count) \
2184 { \
2185 	struct Scsi_Host  *shost = class_to_shost(dev);\
2186 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;\
2187 	uint val = 0;\
2188 	if (!isdigit(buf[0]))\
2189 		return -EINVAL;\
2190 	if (sscanf(buf, "%i", &val) != 1)\
2191 		return -EINVAL;\
2192 	if (lpfc_##attr##_set(vport, val) == 0) \
2193 		return strlen(buf);\
2194 	else \
2195 		return -EINVAL;\
2196 }
2197 
2198 
2199 static DEVICE_ATTR(nvme_info, 0444, lpfc_nvme_info_show, NULL);
2200 static DEVICE_ATTR(bg_info, S_IRUGO, lpfc_bg_info_show, NULL);
2201 static DEVICE_ATTR(bg_guard_err, S_IRUGO, lpfc_bg_guard_err_show, NULL);
2202 static DEVICE_ATTR(bg_apptag_err, S_IRUGO, lpfc_bg_apptag_err_show, NULL);
2203 static DEVICE_ATTR(bg_reftag_err, S_IRUGO, lpfc_bg_reftag_err_show, NULL);
2204 static DEVICE_ATTR(info, S_IRUGO, lpfc_info_show, NULL);
2205 static DEVICE_ATTR(serialnum, S_IRUGO, lpfc_serialnum_show, NULL);
2206 static DEVICE_ATTR(modeldesc, S_IRUGO, lpfc_modeldesc_show, NULL);
2207 static DEVICE_ATTR(modelname, S_IRUGO, lpfc_modelname_show, NULL);
2208 static DEVICE_ATTR(programtype, S_IRUGO, lpfc_programtype_show, NULL);
2209 static DEVICE_ATTR(portnum, S_IRUGO, lpfc_vportnum_show, NULL);
2210 static DEVICE_ATTR(fwrev, S_IRUGO, lpfc_fwrev_show, NULL);
2211 static DEVICE_ATTR(hdw, S_IRUGO, lpfc_hdw_show, NULL);
2212 static DEVICE_ATTR(link_state, S_IRUGO | S_IWUSR, lpfc_link_state_show,
2213 		lpfc_link_state_store);
2214 static DEVICE_ATTR(option_rom_version, S_IRUGO,
2215 		   lpfc_option_rom_version_show, NULL);
2216 static DEVICE_ATTR(num_discovered_ports, S_IRUGO,
2217 		   lpfc_num_discovered_ports_show, NULL);
2218 static DEVICE_ATTR(menlo_mgmt_mode, S_IRUGO, lpfc_mlomgmt_show, NULL);
2219 static DEVICE_ATTR(nport_evt_cnt, S_IRUGO, lpfc_nport_evt_cnt_show, NULL);
2220 static DEVICE_ATTR(lpfc_drvr_version, S_IRUGO, lpfc_drvr_version_show, NULL);
2221 static DEVICE_ATTR(lpfc_enable_fip, S_IRUGO, lpfc_enable_fip_show, NULL);
2222 static DEVICE_ATTR(board_mode, S_IRUGO | S_IWUSR,
2223 		   lpfc_board_mode_show, lpfc_board_mode_store);
2224 static DEVICE_ATTR(issue_reset, S_IWUSR, NULL, lpfc_issue_reset);
2225 static DEVICE_ATTR(max_vpi, S_IRUGO, lpfc_max_vpi_show, NULL);
2226 static DEVICE_ATTR(used_vpi, S_IRUGO, lpfc_used_vpi_show, NULL);
2227 static DEVICE_ATTR(max_rpi, S_IRUGO, lpfc_max_rpi_show, NULL);
2228 static DEVICE_ATTR(used_rpi, S_IRUGO, lpfc_used_rpi_show, NULL);
2229 static DEVICE_ATTR(max_xri, S_IRUGO, lpfc_max_xri_show, NULL);
2230 static DEVICE_ATTR(used_xri, S_IRUGO, lpfc_used_xri_show, NULL);
2231 static DEVICE_ATTR(npiv_info, S_IRUGO, lpfc_npiv_info_show, NULL);
2232 static DEVICE_ATTR(lpfc_temp_sensor, S_IRUGO, lpfc_temp_sensor_show, NULL);
2233 static DEVICE_ATTR(lpfc_fips_level, S_IRUGO, lpfc_fips_level_show, NULL);
2234 static DEVICE_ATTR(lpfc_fips_rev, S_IRUGO, lpfc_fips_rev_show, NULL);
2235 static DEVICE_ATTR(lpfc_dss, S_IRUGO, lpfc_dss_show, NULL);
2236 static DEVICE_ATTR(lpfc_sriov_hw_max_virtfn, S_IRUGO,
2237 		   lpfc_sriov_hw_max_virtfn_show, NULL);
2238 static DEVICE_ATTR(protocol, S_IRUGO, lpfc_sli4_protocol_show, NULL);
2239 static DEVICE_ATTR(lpfc_xlane_supported, S_IRUGO, lpfc_oas_supported_show,
2240 		   NULL);
2241 
2242 static char *lpfc_soft_wwn_key = "C99G71SL8032A";
2243 #define WWN_SZ 8
2244 /**
2245  * lpfc_wwn_set - Convert string to the 8 byte WWN value.
2246  * @buf: WWN string.
2247  * @cnt: Length of string.
2248  * @wwn: Array to receive converted wwn value.
2249  *
2250  * Returns:
2251  * -EINVAL if the buffer does not contain a valid wwn
2252  * 0 success
2253  **/
2254 static size_t
2255 lpfc_wwn_set(const char *buf, size_t cnt, char wwn[])
2256 {
2257 	unsigned int i, j;
2258 
2259 	/* Count may include a LF at end of string */
2260 	if (buf[cnt-1] == '\n')
2261 		cnt--;
2262 
2263 	if ((cnt < 16) || (cnt > 18) || ((cnt == 17) && (*buf++ != 'x')) ||
2264 	    ((cnt == 18) && ((*buf++ != '0') || (*buf++ != 'x'))))
2265 		return -EINVAL;
2266 
2267 	memset(wwn, 0, WWN_SZ);
2268 
2269 	/* Validate and store the new name */
2270 	for (i = 0, j = 0; i < 16; i++) {
2271 		if ((*buf >= 'a') && (*buf <= 'f'))
2272 			j = ((j << 4) | ((*buf++ - 'a') + 10));
2273 		else if ((*buf >= 'A') && (*buf <= 'F'))
2274 			j = ((j << 4) | ((*buf++ - 'A') + 10));
2275 		else if ((*buf >= '0') && (*buf <= '9'))
2276 			j = ((j << 4) | (*buf++ - '0'));
2277 		else
2278 			return -EINVAL;
2279 		if (i % 2) {
2280 			wwn[i/2] = j & 0xff;
2281 			j = 0;
2282 		}
2283 	}
2284 	return 0;
2285 }
2286 /**
2287  * lpfc_soft_wwn_enable_store - Allows setting of the wwn if the key is valid
2288  * @dev: class device that is converted into a Scsi_host.
2289  * @attr: device attribute, not used.
2290  * @buf: containing the string lpfc_soft_wwn_key.
2291  * @count: must be size of lpfc_soft_wwn_key.
2292  *
2293  * Returns:
2294  * -EINVAL if the buffer does not contain lpfc_soft_wwn_key
2295  * length of buf indicates success
2296  **/
2297 static ssize_t
2298 lpfc_soft_wwn_enable_store(struct device *dev, struct device_attribute *attr,
2299 			   const char *buf, size_t count)
2300 {
2301 	struct Scsi_Host  *shost = class_to_shost(dev);
2302 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2303 	struct lpfc_hba   *phba = vport->phba;
2304 	unsigned int cnt = count;
2305 	uint8_t vvvl = vport->fc_sparam.cmn.valid_vendor_ver_level;
2306 	u32 *fawwpn_key = (uint32_t *)&vport->fc_sparam.un.vendorVersion[0];
2307 
2308 	/*
2309 	 * We're doing a simple sanity check for soft_wwpn setting.
2310 	 * We require that the user write a specific key to enable
2311 	 * the soft_wwpn attribute to be settable. Once the attribute
2312 	 * is written, the enable key resets. If further updates are
2313 	 * desired, the key must be written again to re-enable the
2314 	 * attribute.
2315 	 *
2316 	 * The "key" is not secret - it is a hardcoded string shown
2317 	 * here. The intent is to protect against the random user or
2318 	 * application that is just writing attributes.
2319 	 */
2320 	if (vvvl == 1 && cpu_to_be32(*fawwpn_key) == FAPWWN_KEY_VENDOR) {
2321 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2322 				 "0051 "LPFC_DRIVER_NAME" soft wwpn can not"
2323 				 " be enabled: fawwpn is enabled\n");
2324 		return -EINVAL;
2325 	}
2326 
2327 	/* count may include a LF at end of string */
2328 	if (buf[cnt-1] == '\n')
2329 		cnt--;
2330 
2331 	if ((cnt != strlen(lpfc_soft_wwn_key)) ||
2332 	    (strncmp(buf, lpfc_soft_wwn_key, strlen(lpfc_soft_wwn_key)) != 0))
2333 		return -EINVAL;
2334 
2335 	phba->soft_wwn_enable = 1;
2336 
2337 	dev_printk(KERN_WARNING, &phba->pcidev->dev,
2338 		   "lpfc%d: soft_wwpn assignment has been enabled.\n",
2339 		   phba->brd_no);
2340 	dev_printk(KERN_WARNING, &phba->pcidev->dev,
2341 		   "  The soft_wwpn feature is not supported by Broadcom.");
2342 
2343 	return count;
2344 }
2345 static DEVICE_ATTR(lpfc_soft_wwn_enable, S_IWUSR, NULL,
2346 		   lpfc_soft_wwn_enable_store);
2347 
2348 /**
2349  * lpfc_soft_wwpn_show - Return the cfg soft ww port name of the adapter
2350  * @dev: class device that is converted into a Scsi_host.
2351  * @attr: device attribute, not used.
2352  * @buf: on return contains the wwpn in hexadecimal.
2353  *
2354  * Returns: size of formatted string.
2355  **/
2356 static ssize_t
2357 lpfc_soft_wwpn_show(struct device *dev, struct device_attribute *attr,
2358 		    char *buf)
2359 {
2360 	struct Scsi_Host  *shost = class_to_shost(dev);
2361 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2362 	struct lpfc_hba   *phba = vport->phba;
2363 
2364 	return snprintf(buf, PAGE_SIZE, "0x%llx\n",
2365 			(unsigned long long)phba->cfg_soft_wwpn);
2366 }
2367 
2368 /**
2369  * lpfc_soft_wwpn_store - Set the ww port name of the adapter
2370  * @dev class device that is converted into a Scsi_host.
2371  * @attr: device attribute, not used.
2372  * @buf: contains the wwpn in hexadecimal.
2373  * @count: number of wwpn bytes in buf
2374  *
2375  * Returns:
2376  * -EACCES hba reset not enabled, adapter over temp
2377  * -EINVAL soft wwn not enabled, count is invalid, invalid wwpn byte invalid
2378  * -EIO error taking adapter offline or online
2379  * value of count on success
2380  **/
2381 static ssize_t
2382 lpfc_soft_wwpn_store(struct device *dev, struct device_attribute *attr,
2383 		     const char *buf, size_t count)
2384 {
2385 	struct Scsi_Host  *shost = class_to_shost(dev);
2386 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2387 	struct lpfc_hba   *phba = vport->phba;
2388 	struct completion online_compl;
2389 	int stat1 = 0, stat2 = 0;
2390 	unsigned int cnt = count;
2391 	u8 wwpn[WWN_SZ];
2392 	int rc;
2393 
2394 	if (!phba->cfg_enable_hba_reset)
2395 		return -EACCES;
2396 	spin_lock_irq(&phba->hbalock);
2397 	if (phba->over_temp_state == HBA_OVER_TEMP) {
2398 		spin_unlock_irq(&phba->hbalock);
2399 		return -EACCES;
2400 	}
2401 	spin_unlock_irq(&phba->hbalock);
2402 	/* count may include a LF at end of string */
2403 	if (buf[cnt-1] == '\n')
2404 		cnt--;
2405 
2406 	if (!phba->soft_wwn_enable)
2407 		return -EINVAL;
2408 
2409 	/* lock setting wwpn, wwnn down */
2410 	phba->soft_wwn_enable = 0;
2411 
2412 	rc = lpfc_wwn_set(buf, cnt, wwpn);
2413 	if (rc) {
2414 		/* not able to set wwpn, unlock it */
2415 		phba->soft_wwn_enable = 1;
2416 		return rc;
2417 	}
2418 
2419 	phba->cfg_soft_wwpn = wwn_to_u64(wwpn);
2420 	fc_host_port_name(shost) = phba->cfg_soft_wwpn;
2421 	if (phba->cfg_soft_wwnn)
2422 		fc_host_node_name(shost) = phba->cfg_soft_wwnn;
2423 
2424 	dev_printk(KERN_NOTICE, &phba->pcidev->dev,
2425 		   "lpfc%d: Reinitializing to use soft_wwpn\n", phba->brd_no);
2426 
2427 	stat1 = lpfc_do_offline(phba, LPFC_EVT_OFFLINE);
2428 	if (stat1)
2429 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2430 				"0463 lpfc_soft_wwpn attribute set failed to "
2431 				"reinit adapter - %d\n", stat1);
2432 	init_completion(&online_compl);
2433 	rc = lpfc_workq_post_event(phba, &stat2, &online_compl,
2434 				   LPFC_EVT_ONLINE);
2435 	if (rc == 0)
2436 		return -ENOMEM;
2437 
2438 	wait_for_completion(&online_compl);
2439 	if (stat2)
2440 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2441 				"0464 lpfc_soft_wwpn attribute set failed to "
2442 				"reinit adapter - %d\n", stat2);
2443 	return (stat1 || stat2) ? -EIO : count;
2444 }
2445 static DEVICE_ATTR(lpfc_soft_wwpn, S_IRUGO | S_IWUSR,
2446 		   lpfc_soft_wwpn_show, lpfc_soft_wwpn_store);
2447 
2448 /**
2449  * lpfc_soft_wwnn_show - Return the cfg soft ww node name for the adapter
2450  * @dev: class device that is converted into a Scsi_host.
2451  * @attr: device attribute, not used.
2452  * @buf: on return contains the wwnn in hexadecimal.
2453  *
2454  * Returns: size of formatted string.
2455  **/
2456 static ssize_t
2457 lpfc_soft_wwnn_show(struct device *dev, struct device_attribute *attr,
2458 		    char *buf)
2459 {
2460 	struct Scsi_Host *shost = class_to_shost(dev);
2461 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
2462 	return snprintf(buf, PAGE_SIZE, "0x%llx\n",
2463 			(unsigned long long)phba->cfg_soft_wwnn);
2464 }
2465 
2466 /**
2467  * lpfc_soft_wwnn_store - sets the ww node name of the adapter
2468  * @cdev: class device that is converted into a Scsi_host.
2469  * @buf: contains the ww node name in hexadecimal.
2470  * @count: number of wwnn bytes in buf.
2471  *
2472  * Returns:
2473  * -EINVAL soft wwn not enabled, count is invalid, invalid wwnn byte invalid
2474  * value of count on success
2475  **/
2476 static ssize_t
2477 lpfc_soft_wwnn_store(struct device *dev, struct device_attribute *attr,
2478 		     const char *buf, size_t count)
2479 {
2480 	struct Scsi_Host *shost = class_to_shost(dev);
2481 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
2482 	unsigned int cnt = count;
2483 	u8 wwnn[WWN_SZ];
2484 	int rc;
2485 
2486 	/* count may include a LF at end of string */
2487 	if (buf[cnt-1] == '\n')
2488 		cnt--;
2489 
2490 	if (!phba->soft_wwn_enable)
2491 		return -EINVAL;
2492 
2493 	rc = lpfc_wwn_set(buf, cnt, wwnn);
2494 	if (rc) {
2495 		/* Allow wwnn to be set many times, as long as the enable
2496 		 * is set. However, once the wwpn is set, everything locks.
2497 		 */
2498 		return rc;
2499 	}
2500 
2501 	phba->cfg_soft_wwnn = wwn_to_u64(wwnn);
2502 
2503 	dev_printk(KERN_NOTICE, &phba->pcidev->dev,
2504 		   "lpfc%d: soft_wwnn set. Value will take effect upon "
2505 		   "setting of the soft_wwpn\n", phba->brd_no);
2506 
2507 	return count;
2508 }
2509 static DEVICE_ATTR(lpfc_soft_wwnn, S_IRUGO | S_IWUSR,
2510 		   lpfc_soft_wwnn_show, lpfc_soft_wwnn_store);
2511 
2512 /**
2513  * lpfc_oas_tgt_show - Return wwpn of target whose luns maybe enabled for
2514  *		      Optimized Access Storage (OAS) operations.
2515  * @dev: class device that is converted into a Scsi_host.
2516  * @attr: device attribute, not used.
2517  * @buf: buffer for passing information.
2518  *
2519  * Returns:
2520  * value of count
2521  **/
2522 static ssize_t
2523 lpfc_oas_tgt_show(struct device *dev, struct device_attribute *attr,
2524 		  char *buf)
2525 {
2526 	struct Scsi_Host *shost = class_to_shost(dev);
2527 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
2528 
2529 	return snprintf(buf, PAGE_SIZE, "0x%llx\n",
2530 			wwn_to_u64(phba->cfg_oas_tgt_wwpn));
2531 }
2532 
2533 /**
2534  * lpfc_oas_tgt_store - Store wwpn of target whose luns maybe enabled for
2535  *		      Optimized Access Storage (OAS) operations.
2536  * @dev: class device that is converted into a Scsi_host.
2537  * @attr: device attribute, not used.
2538  * @buf: buffer for passing information.
2539  * @count: Size of the data buffer.
2540  *
2541  * Returns:
2542  * -EINVAL count is invalid, invalid wwpn byte invalid
2543  * -EPERM oas is not supported by hba
2544  * value of count on success
2545  **/
2546 static ssize_t
2547 lpfc_oas_tgt_store(struct device *dev, struct device_attribute *attr,
2548 		   const char *buf, size_t count)
2549 {
2550 	struct Scsi_Host *shost = class_to_shost(dev);
2551 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
2552 	unsigned int cnt = count;
2553 	uint8_t wwpn[WWN_SZ];
2554 	int rc;
2555 
2556 	if (!phba->cfg_fof)
2557 		return -EPERM;
2558 
2559 	/* count may include a LF at end of string */
2560 	if (buf[cnt-1] == '\n')
2561 		cnt--;
2562 
2563 	rc = lpfc_wwn_set(buf, cnt, wwpn);
2564 	if (rc)
2565 		return rc;
2566 
2567 	memcpy(phba->cfg_oas_tgt_wwpn, wwpn, (8 * sizeof(uint8_t)));
2568 	memcpy(phba->sli4_hba.oas_next_tgt_wwpn, wwpn, (8 * sizeof(uint8_t)));
2569 	if (wwn_to_u64(wwpn) == 0)
2570 		phba->cfg_oas_flags |= OAS_FIND_ANY_TARGET;
2571 	else
2572 		phba->cfg_oas_flags &= ~OAS_FIND_ANY_TARGET;
2573 	phba->cfg_oas_flags &= ~OAS_LUN_VALID;
2574 	phba->sli4_hba.oas_next_lun = FIND_FIRST_OAS_LUN;
2575 	return count;
2576 }
2577 static DEVICE_ATTR(lpfc_xlane_tgt, S_IRUGO | S_IWUSR,
2578 		   lpfc_oas_tgt_show, lpfc_oas_tgt_store);
2579 
2580 /**
2581  * lpfc_oas_priority_show - Return wwpn of target whose luns maybe enabled for
2582  *		      Optimized Access Storage (OAS) operations.
2583  * @dev: class device that is converted into a Scsi_host.
2584  * @attr: device attribute, not used.
2585  * @buf: buffer for passing information.
2586  *
2587  * Returns:
2588  * value of count
2589  **/
2590 static ssize_t
2591 lpfc_oas_priority_show(struct device *dev, struct device_attribute *attr,
2592 		       char *buf)
2593 {
2594 	struct Scsi_Host *shost = class_to_shost(dev);
2595 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
2596 
2597 	return snprintf(buf, PAGE_SIZE, "%d\n", phba->cfg_oas_priority);
2598 }
2599 
2600 /**
2601  * lpfc_oas_priority_store - Store wwpn of target whose luns maybe enabled for
2602  *		      Optimized Access Storage (OAS) operations.
2603  * @dev: class device that is converted into a Scsi_host.
2604  * @attr: device attribute, not used.
2605  * @buf: buffer for passing information.
2606  * @count: Size of the data buffer.
2607  *
2608  * Returns:
2609  * -EINVAL count is invalid, invalid wwpn byte invalid
2610  * -EPERM oas is not supported by hba
2611  * value of count on success
2612  **/
2613 static ssize_t
2614 lpfc_oas_priority_store(struct device *dev, struct device_attribute *attr,
2615 			const char *buf, size_t count)
2616 {
2617 	struct Scsi_Host *shost = class_to_shost(dev);
2618 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
2619 	unsigned int cnt = count;
2620 	unsigned long val;
2621 	int ret;
2622 
2623 	if (!phba->cfg_fof)
2624 		return -EPERM;
2625 
2626 	/* count may include a LF at end of string */
2627 	if (buf[cnt-1] == '\n')
2628 		cnt--;
2629 
2630 	ret = kstrtoul(buf, 0, &val);
2631 	if (ret || (val > 0x7f))
2632 		return -EINVAL;
2633 
2634 	if (val)
2635 		phba->cfg_oas_priority = (uint8_t)val;
2636 	else
2637 		phba->cfg_oas_priority = phba->cfg_XLanePriority;
2638 	return count;
2639 }
2640 static DEVICE_ATTR(lpfc_xlane_priority, S_IRUGO | S_IWUSR,
2641 		   lpfc_oas_priority_show, lpfc_oas_priority_store);
2642 
2643 /**
2644  * lpfc_oas_vpt_show - Return wwpn of vport whose targets maybe enabled
2645  *		      for Optimized Access Storage (OAS) operations.
2646  * @dev: class device that is converted into a Scsi_host.
2647  * @attr: device attribute, not used.
2648  * @buf: buffer for passing information.
2649  *
2650  * Returns:
2651  * value of count on success
2652  **/
2653 static ssize_t
2654 lpfc_oas_vpt_show(struct device *dev, struct device_attribute *attr,
2655 		  char *buf)
2656 {
2657 	struct Scsi_Host *shost = class_to_shost(dev);
2658 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
2659 
2660 	return snprintf(buf, PAGE_SIZE, "0x%llx\n",
2661 			wwn_to_u64(phba->cfg_oas_vpt_wwpn));
2662 }
2663 
2664 /**
2665  * lpfc_oas_vpt_store - Store wwpn of vport whose targets maybe enabled
2666  *		      for Optimized Access Storage (OAS) operations.
2667  * @dev: class device that is converted into a Scsi_host.
2668  * @attr: device attribute, not used.
2669  * @buf: buffer for passing information.
2670  * @count: Size of the data buffer.
2671  *
2672  * Returns:
2673  * -EINVAL count is invalid, invalid wwpn byte invalid
2674  * -EPERM oas is not supported by hba
2675  * value of count on success
2676  **/
2677 static ssize_t
2678 lpfc_oas_vpt_store(struct device *dev, struct device_attribute *attr,
2679 		   const char *buf, size_t count)
2680 {
2681 	struct Scsi_Host *shost = class_to_shost(dev);
2682 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
2683 	unsigned int cnt = count;
2684 	uint8_t wwpn[WWN_SZ];
2685 	int rc;
2686 
2687 	if (!phba->cfg_fof)
2688 		return -EPERM;
2689 
2690 	/* count may include a LF at end of string */
2691 	if (buf[cnt-1] == '\n')
2692 		cnt--;
2693 
2694 	rc = lpfc_wwn_set(buf, cnt, wwpn);
2695 	if (rc)
2696 		return rc;
2697 
2698 	memcpy(phba->cfg_oas_vpt_wwpn, wwpn, (8 * sizeof(uint8_t)));
2699 	memcpy(phba->sli4_hba.oas_next_vpt_wwpn, wwpn, (8 * sizeof(uint8_t)));
2700 	if (wwn_to_u64(wwpn) == 0)
2701 		phba->cfg_oas_flags |= OAS_FIND_ANY_VPORT;
2702 	else
2703 		phba->cfg_oas_flags &= ~OAS_FIND_ANY_VPORT;
2704 	phba->cfg_oas_flags &= ~OAS_LUN_VALID;
2705 	if (phba->cfg_oas_priority == 0)
2706 		phba->cfg_oas_priority = phba->cfg_XLanePriority;
2707 	phba->sli4_hba.oas_next_lun = FIND_FIRST_OAS_LUN;
2708 	return count;
2709 }
2710 static DEVICE_ATTR(lpfc_xlane_vpt, S_IRUGO | S_IWUSR,
2711 		   lpfc_oas_vpt_show, lpfc_oas_vpt_store);
2712 
2713 /**
2714  * lpfc_oas_lun_state_show - Return the current state (enabled or disabled)
2715  *			    of whether luns will be enabled or disabled
2716  *			    for Optimized Access Storage (OAS) operations.
2717  * @dev: class device that is converted into a Scsi_host.
2718  * @attr: device attribute, not used.
2719  * @buf: buffer for passing information.
2720  *
2721  * Returns:
2722  * size of formatted string.
2723  **/
2724 static ssize_t
2725 lpfc_oas_lun_state_show(struct device *dev, struct device_attribute *attr,
2726 			char *buf)
2727 {
2728 	struct Scsi_Host *shost = class_to_shost(dev);
2729 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
2730 
2731 	return snprintf(buf, PAGE_SIZE, "%d\n", phba->cfg_oas_lun_state);
2732 }
2733 
2734 /**
2735  * lpfc_oas_lun_state_store - Store the state (enabled or disabled)
2736  *			    of whether luns will be enabled or disabled
2737  *			    for Optimized Access Storage (OAS) operations.
2738  * @dev: class device that is converted into a Scsi_host.
2739  * @attr: device attribute, not used.
2740  * @buf: buffer for passing information.
2741  * @count: Size of the data buffer.
2742  *
2743  * Returns:
2744  * -EINVAL count is invalid, invalid wwpn byte invalid
2745  * -EPERM oas is not supported by hba
2746  * value of count on success
2747  **/
2748 static ssize_t
2749 lpfc_oas_lun_state_store(struct device *dev, struct device_attribute *attr,
2750 			 const char *buf, size_t count)
2751 {
2752 	struct Scsi_Host *shost = class_to_shost(dev);
2753 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
2754 	int val = 0;
2755 
2756 	if (!phba->cfg_fof)
2757 		return -EPERM;
2758 
2759 	if (!isdigit(buf[0]))
2760 		return -EINVAL;
2761 
2762 	if (sscanf(buf, "%i", &val) != 1)
2763 		return -EINVAL;
2764 
2765 	if ((val != 0) && (val != 1))
2766 		return -EINVAL;
2767 
2768 	phba->cfg_oas_lun_state = val;
2769 	return strlen(buf);
2770 }
2771 static DEVICE_ATTR(lpfc_xlane_lun_state, S_IRUGO | S_IWUSR,
2772 		   lpfc_oas_lun_state_show, lpfc_oas_lun_state_store);
2773 
2774 /**
2775  * lpfc_oas_lun_status_show - Return the status of the Optimized Access
2776  *                          Storage (OAS) lun returned by the
2777  *                          lpfc_oas_lun_show function.
2778  * @dev: class device that is converted into a Scsi_host.
2779  * @attr: device attribute, not used.
2780  * @buf: buffer for passing information.
2781  *
2782  * Returns:
2783  * size of formatted string.
2784  **/
2785 static ssize_t
2786 lpfc_oas_lun_status_show(struct device *dev, struct device_attribute *attr,
2787 			 char *buf)
2788 {
2789 	struct Scsi_Host *shost = class_to_shost(dev);
2790 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
2791 
2792 	if (!(phba->cfg_oas_flags & OAS_LUN_VALID))
2793 		return -EFAULT;
2794 
2795 	return snprintf(buf, PAGE_SIZE, "%d\n", phba->cfg_oas_lun_status);
2796 }
2797 static DEVICE_ATTR(lpfc_xlane_lun_status, S_IRUGO,
2798 		   lpfc_oas_lun_status_show, NULL);
2799 
2800 
2801 /**
2802  * lpfc_oas_lun_state_set - enable or disable a lun for Optimized Access Storage
2803  *			   (OAS) operations.
2804  * @phba: lpfc_hba pointer.
2805  * @ndlp: pointer to fcp target node.
2806  * @lun: the fc lun for setting oas state.
2807  * @oas_state: the oas state to be set to the lun.
2808  *
2809  * Returns:
2810  * SUCCESS : 0
2811  * -EPERM OAS is not enabled or not supported by this port.
2812  *
2813  */
2814 static size_t
2815 lpfc_oas_lun_state_set(struct lpfc_hba *phba, uint8_t vpt_wwpn[],
2816 		       uint8_t tgt_wwpn[], uint64_t lun,
2817 		       uint32_t oas_state, uint8_t pri)
2818 {
2819 
2820 	int rc = 0;
2821 
2822 	if (!phba->cfg_fof)
2823 		return -EPERM;
2824 
2825 	if (oas_state) {
2826 		if (!lpfc_enable_oas_lun(phba, (struct lpfc_name *)vpt_wwpn,
2827 					 (struct lpfc_name *)tgt_wwpn,
2828 					 lun, pri))
2829 			rc = -ENOMEM;
2830 	} else {
2831 		lpfc_disable_oas_lun(phba, (struct lpfc_name *)vpt_wwpn,
2832 				     (struct lpfc_name *)tgt_wwpn, lun, pri);
2833 	}
2834 	return rc;
2835 
2836 }
2837 
2838 /**
2839  * lpfc_oas_lun_get_next - get the next lun that has been enabled for Optimized
2840  *			  Access Storage (OAS) operations.
2841  * @phba: lpfc_hba pointer.
2842  * @vpt_wwpn: wwpn of the vport associated with the returned lun
2843  * @tgt_wwpn: wwpn of the target associated with the returned lun
2844  * @lun_status: status of the lun returned lun
2845  *
2846  * Returns the first or next lun enabled for OAS operations for the vport/target
2847  * specified.  If a lun is found, its vport wwpn, target wwpn and status is
2848  * returned.  If the lun is not found, NOT_OAS_ENABLED_LUN is returned.
2849  *
2850  * Return:
2851  * lun that is OAS enabled for the vport/target
2852  * NOT_OAS_ENABLED_LUN when no oas enabled lun found.
2853  */
2854 static uint64_t
2855 lpfc_oas_lun_get_next(struct lpfc_hba *phba, uint8_t vpt_wwpn[],
2856 		      uint8_t tgt_wwpn[], uint32_t *lun_status,
2857 		      uint32_t *lun_pri)
2858 {
2859 	uint64_t found_lun;
2860 
2861 	if (unlikely(!phba) || !vpt_wwpn || !tgt_wwpn)
2862 		return NOT_OAS_ENABLED_LUN;
2863 	if (lpfc_find_next_oas_lun(phba, (struct lpfc_name *)
2864 				   phba->sli4_hba.oas_next_vpt_wwpn,
2865 				   (struct lpfc_name *)
2866 				   phba->sli4_hba.oas_next_tgt_wwpn,
2867 				   &phba->sli4_hba.oas_next_lun,
2868 				   (struct lpfc_name *)vpt_wwpn,
2869 				   (struct lpfc_name *)tgt_wwpn,
2870 				   &found_lun, lun_status, lun_pri))
2871 		return found_lun;
2872 	else
2873 		return NOT_OAS_ENABLED_LUN;
2874 }
2875 
2876 /**
2877  * lpfc_oas_lun_state_change - enable/disable a lun for OAS operations
2878  * @phba: lpfc_hba pointer.
2879  * @vpt_wwpn: vport wwpn by reference.
2880  * @tgt_wwpn: target wwpn by reference.
2881  * @lun: the fc lun for setting oas state.
2882  * @oas_state: the oas state to be set to the oas_lun.
2883  *
2884  * This routine enables (OAS_LUN_ENABLE) or disables (OAS_LUN_DISABLE)
2885  * a lun for OAS operations.
2886  *
2887  * Return:
2888  * SUCCESS: 0
2889  * -ENOMEM: failed to enable an lun for OAS operations
2890  * -EPERM: OAS is not enabled
2891  */
2892 static ssize_t
2893 lpfc_oas_lun_state_change(struct lpfc_hba *phba, uint8_t vpt_wwpn[],
2894 			  uint8_t tgt_wwpn[], uint64_t lun,
2895 			  uint32_t oas_state, uint8_t pri)
2896 {
2897 
2898 	int rc;
2899 
2900 	rc = lpfc_oas_lun_state_set(phba, vpt_wwpn, tgt_wwpn, lun,
2901 				    oas_state, pri);
2902 	return rc;
2903 }
2904 
2905 /**
2906  * lpfc_oas_lun_show - Return oas enabled luns from a chosen target
2907  * @dev: class device that is converted into a Scsi_host.
2908  * @attr: device attribute, not used.
2909  * @buf: buffer for passing information.
2910  *
2911  * This routine returns a lun enabled for OAS each time the function
2912  * is called.
2913  *
2914  * Returns:
2915  * SUCCESS: size of formatted string.
2916  * -EFAULT: target or vport wwpn was not set properly.
2917  * -EPERM: oas is not enabled.
2918  **/
2919 static ssize_t
2920 lpfc_oas_lun_show(struct device *dev, struct device_attribute *attr,
2921 		  char *buf)
2922 {
2923 	struct Scsi_Host *shost = class_to_shost(dev);
2924 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
2925 
2926 	uint64_t oas_lun;
2927 	int len = 0;
2928 
2929 	if (!phba->cfg_fof)
2930 		return -EPERM;
2931 
2932 	if (wwn_to_u64(phba->cfg_oas_vpt_wwpn) == 0)
2933 		if (!(phba->cfg_oas_flags & OAS_FIND_ANY_VPORT))
2934 			return -EFAULT;
2935 
2936 	if (wwn_to_u64(phba->cfg_oas_tgt_wwpn) == 0)
2937 		if (!(phba->cfg_oas_flags & OAS_FIND_ANY_TARGET))
2938 			return -EFAULT;
2939 
2940 	oas_lun = lpfc_oas_lun_get_next(phba, phba->cfg_oas_vpt_wwpn,
2941 					phba->cfg_oas_tgt_wwpn,
2942 					&phba->cfg_oas_lun_status,
2943 					&phba->cfg_oas_priority);
2944 	if (oas_lun != NOT_OAS_ENABLED_LUN)
2945 		phba->cfg_oas_flags |= OAS_LUN_VALID;
2946 
2947 	len += snprintf(buf + len, PAGE_SIZE-len, "0x%llx", oas_lun);
2948 
2949 	return len;
2950 }
2951 
2952 /**
2953  * lpfc_oas_lun_store - Sets the OAS state for lun
2954  * @dev: class device that is converted into a Scsi_host.
2955  * @attr: device attribute, not used.
2956  * @buf: buffer for passing information.
2957  *
2958  * This function sets the OAS state for lun.  Before this function is called,
2959  * the vport wwpn, target wwpn, and oas state need to be set.
2960  *
2961  * Returns:
2962  * SUCCESS: size of formatted string.
2963  * -EFAULT: target or vport wwpn was not set properly.
2964  * -EPERM: oas is not enabled.
2965  * size of formatted string.
2966  **/
2967 static ssize_t
2968 lpfc_oas_lun_store(struct device *dev, struct device_attribute *attr,
2969 		   const char *buf, size_t count)
2970 {
2971 	struct Scsi_Host *shost = class_to_shost(dev);
2972 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
2973 	uint64_t scsi_lun;
2974 	uint32_t pri;
2975 	ssize_t rc;
2976 
2977 	if (!phba->cfg_fof)
2978 		return -EPERM;
2979 
2980 	if (wwn_to_u64(phba->cfg_oas_vpt_wwpn) == 0)
2981 		return -EFAULT;
2982 
2983 	if (wwn_to_u64(phba->cfg_oas_tgt_wwpn) == 0)
2984 		return -EFAULT;
2985 
2986 	if (!isdigit(buf[0]))
2987 		return -EINVAL;
2988 
2989 	if (sscanf(buf, "0x%llx", &scsi_lun) != 1)
2990 		return -EINVAL;
2991 
2992 	pri = phba->cfg_oas_priority;
2993 	if (pri == 0)
2994 		pri = phba->cfg_XLanePriority;
2995 
2996 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2997 			"3372 Try to set vport 0x%llx target 0x%llx lun:0x%llx "
2998 			"priority 0x%x with oas state %d\n",
2999 			wwn_to_u64(phba->cfg_oas_vpt_wwpn),
3000 			wwn_to_u64(phba->cfg_oas_tgt_wwpn), scsi_lun,
3001 			pri, phba->cfg_oas_lun_state);
3002 
3003 	rc = lpfc_oas_lun_state_change(phba, phba->cfg_oas_vpt_wwpn,
3004 				       phba->cfg_oas_tgt_wwpn, scsi_lun,
3005 				       phba->cfg_oas_lun_state, pri);
3006 	if (rc)
3007 		return rc;
3008 
3009 	return count;
3010 }
3011 static DEVICE_ATTR(lpfc_xlane_lun, S_IRUGO | S_IWUSR,
3012 		   lpfc_oas_lun_show, lpfc_oas_lun_store);
3013 
3014 int lpfc_enable_nvmet_cnt;
3015 unsigned long long lpfc_enable_nvmet[LPFC_NVMET_MAX_PORTS] = {
3016 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3017 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
3018 module_param_array(lpfc_enable_nvmet, ullong, &lpfc_enable_nvmet_cnt, 0444);
3019 MODULE_PARM_DESC(lpfc_enable_nvmet, "Enable HBA port(s) WWPN as a NVME Target");
3020 
3021 static int lpfc_poll = 0;
3022 module_param(lpfc_poll, int, S_IRUGO);
3023 MODULE_PARM_DESC(lpfc_poll, "FCP ring polling mode control:"
3024 		 " 0 - none,"
3025 		 " 1 - poll with interrupts enabled"
3026 		 " 3 - poll and disable FCP ring interrupts");
3027 
3028 static DEVICE_ATTR(lpfc_poll, S_IRUGO | S_IWUSR,
3029 		   lpfc_poll_show, lpfc_poll_store);
3030 
3031 int lpfc_no_hba_reset_cnt;
3032 unsigned long lpfc_no_hba_reset[MAX_HBAS_NO_RESET] = {
3033 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
3034 module_param_array(lpfc_no_hba_reset, ulong, &lpfc_no_hba_reset_cnt, 0444);
3035 MODULE_PARM_DESC(lpfc_no_hba_reset, "WWPN of HBAs that should not be reset");
3036 
3037 LPFC_ATTR(sli_mode, 0, 0, 3,
3038 	"SLI mode selector:"
3039 	" 0 - auto (SLI-3 if supported),"
3040 	" 2 - select SLI-2 even on SLI-3 capable HBAs,"
3041 	" 3 - select SLI-3");
3042 
3043 LPFC_ATTR_R(enable_npiv, 1, 0, 1,
3044 	"Enable NPIV functionality");
3045 
3046 LPFC_ATTR_R(fcf_failover_policy, 1, 1, 2,
3047 	"FCF Fast failover=1 Priority failover=2");
3048 
3049 /*
3050 # lpfc_enable_rrq: Track XRI/OXID reuse after IO failures
3051 #	0x0 = disabled, XRI/OXID use not tracked.
3052 #	0x1 = XRI/OXID reuse is timed with ratov, RRQ sent.
3053 #	0x2 = XRI/OXID reuse is timed with ratov, No RRQ sent.
3054 */
3055 LPFC_ATTR_R(enable_rrq, 2, 0, 2,
3056 	"Enable RRQ functionality");
3057 
3058 /*
3059 # lpfc_suppress_link_up:  Bring link up at initialization
3060 #            0x0  = bring link up (issue MBX_INIT_LINK)
3061 #            0x1  = do NOT bring link up at initialization(MBX_INIT_LINK)
3062 #            0x2  = never bring up link
3063 # Default value is 0.
3064 */
3065 LPFC_ATTR_R(suppress_link_up, LPFC_INITIALIZE_LINK, LPFC_INITIALIZE_LINK,
3066 		LPFC_DELAY_INIT_LINK_INDEFINITELY,
3067 		"Suppress Link Up at initialization");
3068 /*
3069 # lpfc_cnt: Number of IOCBs allocated for ELS, CT, and ABTS
3070 #       1 - (1024)
3071 #       2 - (2048)
3072 #       3 - (3072)
3073 #       4 - (4096)
3074 #       5 - (5120)
3075 */
3076 static ssize_t
3077 lpfc_iocb_hw_show(struct device *dev, struct device_attribute *attr, char *buf)
3078 {
3079 	struct Scsi_Host  *shost = class_to_shost(dev);
3080 	struct lpfc_hba   *phba = ((struct lpfc_vport *) shost->hostdata)->phba;
3081 
3082 	return snprintf(buf, PAGE_SIZE, "%d\n", phba->iocb_max);
3083 }
3084 
3085 static DEVICE_ATTR(iocb_hw, S_IRUGO,
3086 			 lpfc_iocb_hw_show, NULL);
3087 static ssize_t
3088 lpfc_txq_hw_show(struct device *dev, struct device_attribute *attr, char *buf)
3089 {
3090 	struct Scsi_Host  *shost = class_to_shost(dev);
3091 	struct lpfc_hba   *phba = ((struct lpfc_vport *) shost->hostdata)->phba;
3092 	struct lpfc_sli_ring *pring = lpfc_phba_elsring(phba);
3093 
3094 	return snprintf(buf, PAGE_SIZE, "%d\n", pring->txq_max);
3095 }
3096 
3097 static DEVICE_ATTR(txq_hw, S_IRUGO,
3098 			 lpfc_txq_hw_show, NULL);
3099 static ssize_t
3100 lpfc_txcmplq_hw_show(struct device *dev, struct device_attribute *attr,
3101  char *buf)
3102 {
3103 	struct Scsi_Host  *shost = class_to_shost(dev);
3104 	struct lpfc_hba   *phba = ((struct lpfc_vport *) shost->hostdata)->phba;
3105 	struct lpfc_sli_ring *pring = lpfc_phba_elsring(phba);
3106 
3107 	return snprintf(buf, PAGE_SIZE, "%d\n", pring->txcmplq_max);
3108 }
3109 
3110 static DEVICE_ATTR(txcmplq_hw, S_IRUGO,
3111 			 lpfc_txcmplq_hw_show, NULL);
3112 
3113 LPFC_ATTR_R(iocb_cnt, 2, 1, 5,
3114 	"Number of IOCBs alloc for ELS, CT, and ABTS: 1k to 5k IOCBs");
3115 
3116 /*
3117 # lpfc_nodev_tmo: If set, it will hold all I/O errors on devices that disappear
3118 # until the timer expires. Value range is [0,255]. Default value is 30.
3119 */
3120 static int lpfc_nodev_tmo = LPFC_DEF_DEVLOSS_TMO;
3121 static int lpfc_devloss_tmo = LPFC_DEF_DEVLOSS_TMO;
3122 module_param(lpfc_nodev_tmo, int, 0);
3123 MODULE_PARM_DESC(lpfc_nodev_tmo,
3124 		 "Seconds driver will hold I/O waiting "
3125 		 "for a device to come back");
3126 
3127 /**
3128  * lpfc_nodev_tmo_show - Return the hba dev loss timeout value
3129  * @dev: class converted to a Scsi_host structure.
3130  * @attr: device attribute, not used.
3131  * @buf: on return contains the dev loss timeout in decimal.
3132  *
3133  * Returns: size of formatted string.
3134  **/
3135 static ssize_t
3136 lpfc_nodev_tmo_show(struct device *dev, struct device_attribute *attr,
3137 		    char *buf)
3138 {
3139 	struct Scsi_Host  *shost = class_to_shost(dev);
3140 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3141 
3142 	return snprintf(buf, PAGE_SIZE, "%d\n",	vport->cfg_devloss_tmo);
3143 }
3144 
3145 /**
3146  * lpfc_nodev_tmo_init - Set the hba nodev timeout value
3147  * @vport: lpfc vport structure pointer.
3148  * @val: contains the nodev timeout value.
3149  *
3150  * Description:
3151  * If the devloss tmo is already set then nodev tmo is set to devloss tmo,
3152  * a kernel error message is printed and zero is returned.
3153  * Else if val is in range then nodev tmo and devloss tmo are set to val.
3154  * Otherwise nodev tmo is set to the default value.
3155  *
3156  * Returns:
3157  * zero if already set or if val is in range
3158  * -EINVAL val out of range
3159  **/
3160 static int
3161 lpfc_nodev_tmo_init(struct lpfc_vport *vport, int val)
3162 {
3163 	if (vport->cfg_devloss_tmo != LPFC_DEF_DEVLOSS_TMO) {
3164 		vport->cfg_nodev_tmo = vport->cfg_devloss_tmo;
3165 		if (val != LPFC_DEF_DEVLOSS_TMO)
3166 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3167 					 "0407 Ignoring lpfc_nodev_tmo module "
3168 					 "parameter because lpfc_devloss_tmo "
3169 					 "is set.\n");
3170 		return 0;
3171 	}
3172 
3173 	if (val >= LPFC_MIN_DEVLOSS_TMO && val <= LPFC_MAX_DEVLOSS_TMO) {
3174 		vport->cfg_nodev_tmo = val;
3175 		vport->cfg_devloss_tmo = val;
3176 		return 0;
3177 	}
3178 	lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3179 			 "0400 lpfc_nodev_tmo attribute cannot be set to"
3180 			 " %d, allowed range is [%d, %d]\n",
3181 			 val, LPFC_MIN_DEVLOSS_TMO, LPFC_MAX_DEVLOSS_TMO);
3182 	vport->cfg_nodev_tmo = LPFC_DEF_DEVLOSS_TMO;
3183 	return -EINVAL;
3184 }
3185 
3186 /**
3187  * lpfc_update_rport_devloss_tmo - Update dev loss tmo value
3188  * @vport: lpfc vport structure pointer.
3189  *
3190  * Description:
3191  * Update all the ndlp's dev loss tmo with the vport devloss tmo value.
3192  **/
3193 static void
3194 lpfc_update_rport_devloss_tmo(struct lpfc_vport *vport)
3195 {
3196 	struct Scsi_Host  *shost;
3197 	struct lpfc_nodelist  *ndlp;
3198 
3199 	shost = lpfc_shost_from_vport(vport);
3200 	spin_lock_irq(shost->host_lock);
3201 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp)
3202 		if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport)
3203 			ndlp->rport->dev_loss_tmo = vport->cfg_devloss_tmo;
3204 	spin_unlock_irq(shost->host_lock);
3205 }
3206 
3207 /**
3208  * lpfc_nodev_tmo_set - Set the vport nodev tmo and devloss tmo values
3209  * @vport: lpfc vport structure pointer.
3210  * @val: contains the tmo value.
3211  *
3212  * Description:
3213  * If the devloss tmo is already set or the vport dev loss tmo has changed
3214  * then a kernel error message is printed and zero is returned.
3215  * Else if val is in range then nodev tmo and devloss tmo are set to val.
3216  * Otherwise nodev tmo is set to the default value.
3217  *
3218  * Returns:
3219  * zero if already set or if val is in range
3220  * -EINVAL val out of range
3221  **/
3222 static int
3223 lpfc_nodev_tmo_set(struct lpfc_vport *vport, int val)
3224 {
3225 	if (vport->dev_loss_tmo_changed ||
3226 	    (lpfc_devloss_tmo != LPFC_DEF_DEVLOSS_TMO)) {
3227 		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3228 				 "0401 Ignoring change to lpfc_nodev_tmo "
3229 				 "because lpfc_devloss_tmo is set.\n");
3230 		return 0;
3231 	}
3232 	if (val >= LPFC_MIN_DEVLOSS_TMO && val <= LPFC_MAX_DEVLOSS_TMO) {
3233 		vport->cfg_nodev_tmo = val;
3234 		vport->cfg_devloss_tmo = val;
3235 		/*
3236 		 * For compat: set the fc_host dev loss so new rports
3237 		 * will get the value.
3238 		 */
3239 		fc_host_dev_loss_tmo(lpfc_shost_from_vport(vport)) = val;
3240 		lpfc_update_rport_devloss_tmo(vport);
3241 		return 0;
3242 	}
3243 	lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3244 			 "0403 lpfc_nodev_tmo attribute cannot be set to "
3245 			 "%d, allowed range is [%d, %d]\n",
3246 			 val, LPFC_MIN_DEVLOSS_TMO, LPFC_MAX_DEVLOSS_TMO);
3247 	return -EINVAL;
3248 }
3249 
3250 lpfc_vport_param_store(nodev_tmo)
3251 
3252 static DEVICE_ATTR(lpfc_nodev_tmo, S_IRUGO | S_IWUSR,
3253 		   lpfc_nodev_tmo_show, lpfc_nodev_tmo_store);
3254 
3255 /*
3256 # lpfc_devloss_tmo: If set, it will hold all I/O errors on devices that
3257 # disappear until the timer expires. Value range is [0,255]. Default
3258 # value is 30.
3259 */
3260 module_param(lpfc_devloss_tmo, int, S_IRUGO);
3261 MODULE_PARM_DESC(lpfc_devloss_tmo,
3262 		 "Seconds driver will hold I/O waiting "
3263 		 "for a device to come back");
3264 lpfc_vport_param_init(devloss_tmo, LPFC_DEF_DEVLOSS_TMO,
3265 		      LPFC_MIN_DEVLOSS_TMO, LPFC_MAX_DEVLOSS_TMO)
3266 lpfc_vport_param_show(devloss_tmo)
3267 
3268 /**
3269  * lpfc_devloss_tmo_set - Sets vport nodev tmo, devloss tmo values, changed bit
3270  * @vport: lpfc vport structure pointer.
3271  * @val: contains the tmo value.
3272  *
3273  * Description:
3274  * If val is in a valid range then set the vport nodev tmo,
3275  * devloss tmo, also set the vport dev loss tmo changed flag.
3276  * Else a kernel error message is printed.
3277  *
3278  * Returns:
3279  * zero if val is in range
3280  * -EINVAL val out of range
3281  **/
3282 static int
3283 lpfc_devloss_tmo_set(struct lpfc_vport *vport, int val)
3284 {
3285 	if (val >= LPFC_MIN_DEVLOSS_TMO && val <= LPFC_MAX_DEVLOSS_TMO) {
3286 		vport->cfg_nodev_tmo = val;
3287 		vport->cfg_devloss_tmo = val;
3288 		vport->dev_loss_tmo_changed = 1;
3289 		fc_host_dev_loss_tmo(lpfc_shost_from_vport(vport)) = val;
3290 		lpfc_update_rport_devloss_tmo(vport);
3291 		return 0;
3292 	}
3293 
3294 	lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3295 			 "0404 lpfc_devloss_tmo attribute cannot be set to "
3296 			 "%d, allowed range is [%d, %d]\n",
3297 			 val, LPFC_MIN_DEVLOSS_TMO, LPFC_MAX_DEVLOSS_TMO);
3298 	return -EINVAL;
3299 }
3300 
3301 lpfc_vport_param_store(devloss_tmo)
3302 static DEVICE_ATTR(lpfc_devloss_tmo, S_IRUGO | S_IWUSR,
3303 		   lpfc_devloss_tmo_show, lpfc_devloss_tmo_store);
3304 
3305 /*
3306  * lpfc_suppress_rsp: Enable suppress rsp feature is firmware supports it
3307  * lpfc_suppress_rsp = 0  Disable
3308  * lpfc_suppress_rsp = 1  Enable (default)
3309  *
3310  */
3311 LPFC_ATTR_R(suppress_rsp, 1, 0, 1,
3312 	    "Enable suppress rsp feature is firmware supports it");
3313 
3314 /*
3315  * lpfc_nvmet_mrq: Specify number of RQ pairs for processing NVMET cmds
3316  * lpfc_nvmet_mrq = 1  use a single RQ pair
3317  * lpfc_nvmet_mrq >= 2  use specified RQ pairs for MRQ
3318  *
3319  */
3320 LPFC_ATTR_R(nvmet_mrq,
3321 	    1, 1, 16,
3322 	    "Specify number of RQ pairs for processing NVMET cmds");
3323 
3324 /*
3325  * lpfc_enable_fc4_type: Defines what FC4 types are supported.
3326  * Supported Values:  1 - register just FCP
3327  *                    3 - register both FCP and NVME
3328  * Supported values are [1,3]. Default value is 1
3329  */
3330 LPFC_ATTR_R(enable_fc4_type, LPFC_ENABLE_FCP,
3331 	    LPFC_ENABLE_FCP, LPFC_ENABLE_BOTH,
3332 	    "Define fc4 type to register with fabric.");
3333 
3334 /*
3335  * lpfc_xri_split: Defines the division of XRI resources between SCSI and NVME
3336  * This parameter is only used if:
3337  *     lpfc_enable_fc4_type is 3 - register both FCP and NVME and
3338  *     port is not configured for NVMET.
3339  *
3340  * ELS/CT always get 10% of XRIs, up to a maximum of 250
3341  * The remaining XRIs get split up based on lpfc_xri_split per port:
3342  *
3343  * Supported Values are in percentages
3344  * the xri_split value is the percentage the SCSI port will get. The remaining
3345  * percentage will go to NVME.
3346  */
3347 LPFC_ATTR_R(xri_split, 50, 10, 90,
3348 	    "Division of XRI resources between SCSI and NVME");
3349 
3350 /*
3351 # lpfc_log_verbose: Only turn this flag on if you are willing to risk being
3352 # deluged with LOTS of information.
3353 # You can set a bit mask to record specific types of verbose messages:
3354 # See lpfc_logmsh.h for definitions.
3355 */
3356 LPFC_VPORT_ATTR_HEX_RW(log_verbose, 0x0, 0x0, 0xffffffff,
3357 		       "Verbose logging bit-mask");
3358 
3359 /*
3360 # lpfc_enable_da_id: This turns on the DA_ID CT command that deregisters
3361 # objects that have been registered with the nameserver after login.
3362 */
3363 LPFC_VPORT_ATTR_R(enable_da_id, 1, 0, 1,
3364 		  "Deregister nameserver objects before LOGO");
3365 
3366 /*
3367 # lun_queue_depth:  This parameter is used to limit the number of outstanding
3368 # commands per FCP LUN. Value range is [1,512]. Default value is 30.
3369 # If this parameter value is greater than 1/8th the maximum number of exchanges
3370 # supported by the HBA port, then the lun queue depth will be reduced to
3371 # 1/8th the maximum number of exchanges.
3372 */
3373 LPFC_VPORT_ATTR_R(lun_queue_depth, 30, 1, 512,
3374 		  "Max number of FCP commands we can queue to a specific LUN");
3375 
3376 /*
3377 # tgt_queue_depth:  This parameter is used to limit the number of outstanding
3378 # commands per target port. Value range is [10,65535]. Default value is 65535.
3379 */
3380 LPFC_VPORT_ATTR_R(tgt_queue_depth, 65535, 10, 65535,
3381 		  "Max number of FCP commands we can queue to a specific target port");
3382 
3383 /*
3384 # hba_queue_depth:  This parameter is used to limit the number of outstanding
3385 # commands per lpfc HBA. Value range is [32,8192]. If this parameter
3386 # value is greater than the maximum number of exchanges supported by the HBA,
3387 # then maximum number of exchanges supported by the HBA is used to determine
3388 # the hba_queue_depth.
3389 */
3390 LPFC_ATTR_R(hba_queue_depth, 8192, 32, 8192,
3391 	    "Max number of FCP commands we can queue to a lpfc HBA");
3392 
3393 /*
3394 # peer_port_login:  This parameter allows/prevents logins
3395 # between peer ports hosted on the same physical port.
3396 # When this parameter is set 0 peer ports of same physical port
3397 # are not allowed to login to each other.
3398 # When this parameter is set 1 peer ports of same physical port
3399 # are allowed to login to each other.
3400 # Default value of this parameter is 0.
3401 */
3402 LPFC_VPORT_ATTR_R(peer_port_login, 0, 0, 1,
3403 		  "Allow peer ports on the same physical port to login to each "
3404 		  "other.");
3405 
3406 /*
3407 # restrict_login:  This parameter allows/prevents logins
3408 # between Virtual Ports and remote initiators.
3409 # When this parameter is not set (0) Virtual Ports will accept PLOGIs from
3410 # other initiators and will attempt to PLOGI all remote ports.
3411 # When this parameter is set (1) Virtual Ports will reject PLOGIs from
3412 # remote ports and will not attempt to PLOGI to other initiators.
3413 # This parameter does not restrict to the physical port.
3414 # This parameter does not restrict logins to Fabric resident remote ports.
3415 # Default value of this parameter is 1.
3416 */
3417 static int lpfc_restrict_login = 1;
3418 module_param(lpfc_restrict_login, int, S_IRUGO);
3419 MODULE_PARM_DESC(lpfc_restrict_login,
3420 		 "Restrict virtual ports login to remote initiators.");
3421 lpfc_vport_param_show(restrict_login);
3422 
3423 /**
3424  * lpfc_restrict_login_init - Set the vport restrict login flag
3425  * @vport: lpfc vport structure pointer.
3426  * @val: contains the restrict login value.
3427  *
3428  * Description:
3429  * If val is not in a valid range then log a kernel error message and set
3430  * the vport restrict login to one.
3431  * If the port type is physical clear the restrict login flag and return.
3432  * Else set the restrict login flag to val.
3433  *
3434  * Returns:
3435  * zero if val is in range
3436  * -EINVAL val out of range
3437  **/
3438 static int
3439 lpfc_restrict_login_init(struct lpfc_vport *vport, int val)
3440 {
3441 	if (val < 0 || val > 1) {
3442 		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3443 				 "0422 lpfc_restrict_login attribute cannot "
3444 				 "be set to %d, allowed range is [0, 1]\n",
3445 				 val);
3446 		vport->cfg_restrict_login = 1;
3447 		return -EINVAL;
3448 	}
3449 	if (vport->port_type == LPFC_PHYSICAL_PORT) {
3450 		vport->cfg_restrict_login = 0;
3451 		return 0;
3452 	}
3453 	vport->cfg_restrict_login = val;
3454 	return 0;
3455 }
3456 
3457 /**
3458  * lpfc_restrict_login_set - Set the vport restrict login flag
3459  * @vport: lpfc vport structure pointer.
3460  * @val: contains the restrict login value.
3461  *
3462  * Description:
3463  * If val is not in a valid range then log a kernel error message and set
3464  * the vport restrict login to one.
3465  * If the port type is physical and the val is not zero log a kernel
3466  * error message, clear the restrict login flag and return zero.
3467  * Else set the restrict login flag to val.
3468  *
3469  * Returns:
3470  * zero if val is in range
3471  * -EINVAL val out of range
3472  **/
3473 static int
3474 lpfc_restrict_login_set(struct lpfc_vport *vport, int val)
3475 {
3476 	if (val < 0 || val > 1) {
3477 		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3478 				 "0425 lpfc_restrict_login attribute cannot "
3479 				 "be set to %d, allowed range is [0, 1]\n",
3480 				 val);
3481 		vport->cfg_restrict_login = 1;
3482 		return -EINVAL;
3483 	}
3484 	if (vport->port_type == LPFC_PHYSICAL_PORT && val != 0) {
3485 		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3486 				 "0468 lpfc_restrict_login must be 0 for "
3487 				 "Physical ports.\n");
3488 		vport->cfg_restrict_login = 0;
3489 		return 0;
3490 	}
3491 	vport->cfg_restrict_login = val;
3492 	return 0;
3493 }
3494 lpfc_vport_param_store(restrict_login);
3495 static DEVICE_ATTR(lpfc_restrict_login, S_IRUGO | S_IWUSR,
3496 		   lpfc_restrict_login_show, lpfc_restrict_login_store);
3497 
3498 /*
3499 # Some disk devices have a "select ID" or "select Target" capability.
3500 # From a protocol standpoint "select ID" usually means select the
3501 # Fibre channel "ALPA".  In the FC-AL Profile there is an "informative
3502 # annex" which contains a table that maps a "select ID" (a number
3503 # between 0 and 7F) to an ALPA.  By default, for compatibility with
3504 # older drivers, the lpfc driver scans this table from low ALPA to high
3505 # ALPA.
3506 #
3507 # Turning on the scan-down variable (on  = 1, off = 0) will
3508 # cause the lpfc driver to use an inverted table, effectively
3509 # scanning ALPAs from high to low. Value range is [0,1]. Default value is 1.
3510 #
3511 # (Note: This "select ID" functionality is a LOOP ONLY characteristic
3512 # and will not work across a fabric. Also this parameter will take
3513 # effect only in the case when ALPA map is not available.)
3514 */
3515 LPFC_VPORT_ATTR_R(scan_down, 1, 0, 1,
3516 		  "Start scanning for devices from highest ALPA to lowest");
3517 
3518 /*
3519 # lpfc_topology:  link topology for init link
3520 #            0x0  = attempt loop mode then point-to-point
3521 #            0x01 = internal loopback mode
3522 #            0x02 = attempt point-to-point mode only
3523 #            0x04 = attempt loop mode only
3524 #            0x06 = attempt point-to-point mode then loop
3525 # Set point-to-point mode if you want to run as an N_Port.
3526 # Set loop mode if you want to run as an NL_Port. Value range is [0,0x6].
3527 # Default value is 0.
3528 */
3529 LPFC_ATTR(topology, 0, 0, 6,
3530 	"Select Fibre Channel topology");
3531 
3532 /**
3533  * lpfc_topology_set - Set the adapters topology field
3534  * @phba: lpfc_hba pointer.
3535  * @val: topology value.
3536  *
3537  * Description:
3538  * If val is in a valid range then set the adapter's topology field and
3539  * issue a lip; if the lip fails reset the topology to the old value.
3540  *
3541  * If the value is not in range log a kernel error message and return an error.
3542  *
3543  * Returns:
3544  * zero if val is in range and lip okay
3545  * non-zero return value from lpfc_issue_lip()
3546  * -EINVAL val out of range
3547  **/
3548 static ssize_t
3549 lpfc_topology_store(struct device *dev, struct device_attribute *attr,
3550 			const char *buf, size_t count)
3551 {
3552 	struct Scsi_Host  *shost = class_to_shost(dev);
3553 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3554 	struct lpfc_hba   *phba = vport->phba;
3555 	int val = 0;
3556 	int nolip = 0;
3557 	const char *val_buf = buf;
3558 	int err;
3559 	uint32_t prev_val;
3560 
3561 	if (!strncmp(buf, "nolip ", strlen("nolip "))) {
3562 		nolip = 1;
3563 		val_buf = &buf[strlen("nolip ")];
3564 	}
3565 
3566 	if (!isdigit(val_buf[0]))
3567 		return -EINVAL;
3568 	if (sscanf(val_buf, "%i", &val) != 1)
3569 		return -EINVAL;
3570 
3571 	if (val >= 0 && val <= 6) {
3572 		prev_val = phba->cfg_topology;
3573 		if (phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G &&
3574 			val == 4) {
3575 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3576 				"3113 Loop mode not supported at speed %d\n",
3577 				val);
3578 			return -EINVAL;
3579 		}
3580 		if (phba->pcidev->device == PCI_DEVICE_ID_LANCER_G6_FC &&
3581 			val == 4) {
3582 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3583 				"3114 Loop mode not supported\n");
3584 			return -EINVAL;
3585 		}
3586 		phba->cfg_topology = val;
3587 		if (nolip)
3588 			return strlen(buf);
3589 
3590 		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3591 			"3054 lpfc_topology changed from %d to %d\n",
3592 			prev_val, val);
3593 		if (prev_val != val && phba->sli_rev == LPFC_SLI_REV4)
3594 			phba->fc_topology_changed = 1;
3595 		err = lpfc_issue_lip(lpfc_shost_from_vport(phba->pport));
3596 		if (err) {
3597 			phba->cfg_topology = prev_val;
3598 			return -EINVAL;
3599 		} else
3600 			return strlen(buf);
3601 	}
3602 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3603 		"%d:0467 lpfc_topology attribute cannot be set to %d, "
3604 		"allowed range is [0, 6]\n",
3605 		phba->brd_no, val);
3606 	return -EINVAL;
3607 }
3608 
3609 lpfc_param_show(topology)
3610 static DEVICE_ATTR(lpfc_topology, S_IRUGO | S_IWUSR,
3611 		lpfc_topology_show, lpfc_topology_store);
3612 
3613 /**
3614  * lpfc_static_vport_show: Read callback function for
3615  *   lpfc_static_vport sysfs file.
3616  * @dev: Pointer to class device object.
3617  * @attr: device attribute structure.
3618  * @buf: Data buffer.
3619  *
3620  * This function is the read call back function for
3621  * lpfc_static_vport sysfs file. The lpfc_static_vport
3622  * sysfs file report the mageability of the vport.
3623  **/
3624 static ssize_t
3625 lpfc_static_vport_show(struct device *dev, struct device_attribute *attr,
3626 			 char *buf)
3627 {
3628 	struct Scsi_Host  *shost = class_to_shost(dev);
3629 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3630 	if (vport->vport_flag & STATIC_VPORT)
3631 		sprintf(buf, "1\n");
3632 	else
3633 		sprintf(buf, "0\n");
3634 
3635 	return strlen(buf);
3636 }
3637 
3638 /*
3639  * Sysfs attribute to control the statistical data collection.
3640  */
3641 static DEVICE_ATTR(lpfc_static_vport, S_IRUGO,
3642 		   lpfc_static_vport_show, NULL);
3643 
3644 /**
3645  * lpfc_stat_data_ctrl_store - write call back for lpfc_stat_data_ctrl sysfs file
3646  * @dev: Pointer to class device.
3647  * @buf: Data buffer.
3648  * @count: Size of the data buffer.
3649  *
3650  * This function get called when a user write to the lpfc_stat_data_ctrl
3651  * sysfs file. This function parse the command written to the sysfs file
3652  * and take appropriate action. These commands are used for controlling
3653  * driver statistical data collection.
3654  * Following are the command this function handles.
3655  *
3656  *    setbucket <bucket_type> <base> <step>
3657  *			       = Set the latency buckets.
3658  *    destroybucket            = destroy all the buckets.
3659  *    start                    = start data collection
3660  *    stop                     = stop data collection
3661  *    reset                    = reset the collected data
3662  **/
3663 static ssize_t
3664 lpfc_stat_data_ctrl_store(struct device *dev, struct device_attribute *attr,
3665 			  const char *buf, size_t count)
3666 {
3667 	struct Scsi_Host  *shost = class_to_shost(dev);
3668 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3669 	struct lpfc_hba   *phba = vport->phba;
3670 #define LPFC_MAX_DATA_CTRL_LEN 1024
3671 	static char bucket_data[LPFC_MAX_DATA_CTRL_LEN];
3672 	unsigned long i;
3673 	char *str_ptr, *token;
3674 	struct lpfc_vport **vports;
3675 	struct Scsi_Host *v_shost;
3676 	char *bucket_type_str, *base_str, *step_str;
3677 	unsigned long base, step, bucket_type;
3678 
3679 	if (!strncmp(buf, "setbucket", strlen("setbucket"))) {
3680 		if (strlen(buf) > (LPFC_MAX_DATA_CTRL_LEN - 1))
3681 			return -EINVAL;
3682 
3683 		strncpy(bucket_data, buf, LPFC_MAX_DATA_CTRL_LEN);
3684 		str_ptr = &bucket_data[0];
3685 		/* Ignore this token - this is command token */
3686 		token = strsep(&str_ptr, "\t ");
3687 		if (!token)
3688 			return -EINVAL;
3689 
3690 		bucket_type_str = strsep(&str_ptr, "\t ");
3691 		if (!bucket_type_str)
3692 			return -EINVAL;
3693 
3694 		if (!strncmp(bucket_type_str, "linear", strlen("linear")))
3695 			bucket_type = LPFC_LINEAR_BUCKET;
3696 		else if (!strncmp(bucket_type_str, "power2", strlen("power2")))
3697 			bucket_type = LPFC_POWER2_BUCKET;
3698 		else
3699 			return -EINVAL;
3700 
3701 		base_str = strsep(&str_ptr, "\t ");
3702 		if (!base_str)
3703 			return -EINVAL;
3704 		base = simple_strtoul(base_str, NULL, 0);
3705 
3706 		step_str = strsep(&str_ptr, "\t ");
3707 		if (!step_str)
3708 			return -EINVAL;
3709 		step = simple_strtoul(step_str, NULL, 0);
3710 		if (!step)
3711 			return -EINVAL;
3712 
3713 		/* Block the data collection for every vport */
3714 		vports = lpfc_create_vport_work_array(phba);
3715 		if (vports == NULL)
3716 			return -ENOMEM;
3717 
3718 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3719 			v_shost = lpfc_shost_from_vport(vports[i]);
3720 			spin_lock_irq(v_shost->host_lock);
3721 			/* Block and reset data collection */
3722 			vports[i]->stat_data_blocked = 1;
3723 			if (vports[i]->stat_data_enabled)
3724 				lpfc_vport_reset_stat_data(vports[i]);
3725 			spin_unlock_irq(v_shost->host_lock);
3726 		}
3727 
3728 		/* Set the bucket attributes */
3729 		phba->bucket_type = bucket_type;
3730 		phba->bucket_base = base;
3731 		phba->bucket_step = step;
3732 
3733 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3734 			v_shost = lpfc_shost_from_vport(vports[i]);
3735 
3736 			/* Unblock data collection */
3737 			spin_lock_irq(v_shost->host_lock);
3738 			vports[i]->stat_data_blocked = 0;
3739 			spin_unlock_irq(v_shost->host_lock);
3740 		}
3741 		lpfc_destroy_vport_work_array(phba, vports);
3742 		return strlen(buf);
3743 	}
3744 
3745 	if (!strncmp(buf, "destroybucket", strlen("destroybucket"))) {
3746 		vports = lpfc_create_vport_work_array(phba);
3747 		if (vports == NULL)
3748 			return -ENOMEM;
3749 
3750 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3751 			v_shost = lpfc_shost_from_vport(vports[i]);
3752 			spin_lock_irq(shost->host_lock);
3753 			vports[i]->stat_data_blocked = 1;
3754 			lpfc_free_bucket(vport);
3755 			vport->stat_data_enabled = 0;
3756 			vports[i]->stat_data_blocked = 0;
3757 			spin_unlock_irq(shost->host_lock);
3758 		}
3759 		lpfc_destroy_vport_work_array(phba, vports);
3760 		phba->bucket_type = LPFC_NO_BUCKET;
3761 		phba->bucket_base = 0;
3762 		phba->bucket_step = 0;
3763 		return strlen(buf);
3764 	}
3765 
3766 	if (!strncmp(buf, "start", strlen("start"))) {
3767 		/* If no buckets configured return error */
3768 		if (phba->bucket_type == LPFC_NO_BUCKET)
3769 			return -EINVAL;
3770 		spin_lock_irq(shost->host_lock);
3771 		if (vport->stat_data_enabled) {
3772 			spin_unlock_irq(shost->host_lock);
3773 			return strlen(buf);
3774 		}
3775 		lpfc_alloc_bucket(vport);
3776 		vport->stat_data_enabled = 1;
3777 		spin_unlock_irq(shost->host_lock);
3778 		return strlen(buf);
3779 	}
3780 
3781 	if (!strncmp(buf, "stop", strlen("stop"))) {
3782 		spin_lock_irq(shost->host_lock);
3783 		if (vport->stat_data_enabled == 0) {
3784 			spin_unlock_irq(shost->host_lock);
3785 			return strlen(buf);
3786 		}
3787 		lpfc_free_bucket(vport);
3788 		vport->stat_data_enabled = 0;
3789 		spin_unlock_irq(shost->host_lock);
3790 		return strlen(buf);
3791 	}
3792 
3793 	if (!strncmp(buf, "reset", strlen("reset"))) {
3794 		if ((phba->bucket_type == LPFC_NO_BUCKET)
3795 			|| !vport->stat_data_enabled)
3796 			return strlen(buf);
3797 		spin_lock_irq(shost->host_lock);
3798 		vport->stat_data_blocked = 1;
3799 		lpfc_vport_reset_stat_data(vport);
3800 		vport->stat_data_blocked = 0;
3801 		spin_unlock_irq(shost->host_lock);
3802 		return strlen(buf);
3803 	}
3804 	return -EINVAL;
3805 }
3806 
3807 
3808 /**
3809  * lpfc_stat_data_ctrl_show - Read function for lpfc_stat_data_ctrl sysfs file
3810  * @dev: Pointer to class device object.
3811  * @buf: Data buffer.
3812  *
3813  * This function is the read call back function for
3814  * lpfc_stat_data_ctrl sysfs file. This function report the
3815  * current statistical data collection state.
3816  **/
3817 static ssize_t
3818 lpfc_stat_data_ctrl_show(struct device *dev, struct device_attribute *attr,
3819 			 char *buf)
3820 {
3821 	struct Scsi_Host  *shost = class_to_shost(dev);
3822 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3823 	struct lpfc_hba   *phba = vport->phba;
3824 	int index = 0;
3825 	int i;
3826 	char *bucket_type;
3827 	unsigned long bucket_value;
3828 
3829 	switch (phba->bucket_type) {
3830 	case LPFC_LINEAR_BUCKET:
3831 		bucket_type = "linear";
3832 		break;
3833 	case LPFC_POWER2_BUCKET:
3834 		bucket_type = "power2";
3835 		break;
3836 	default:
3837 		bucket_type = "No Bucket";
3838 		break;
3839 	}
3840 
3841 	sprintf(&buf[index], "Statistical Data enabled :%d, "
3842 		"blocked :%d, Bucket type :%s, Bucket base :%d,"
3843 		" Bucket step :%d\nLatency Ranges :",
3844 		vport->stat_data_enabled, vport->stat_data_blocked,
3845 		bucket_type, phba->bucket_base, phba->bucket_step);
3846 	index = strlen(buf);
3847 	if (phba->bucket_type != LPFC_NO_BUCKET) {
3848 		for (i = 0; i < LPFC_MAX_BUCKET_COUNT; i++) {
3849 			if (phba->bucket_type == LPFC_LINEAR_BUCKET)
3850 				bucket_value = phba->bucket_base +
3851 					phba->bucket_step * i;
3852 			else
3853 				bucket_value = phba->bucket_base +
3854 				(1 << i) * phba->bucket_step;
3855 
3856 			if (index + 10 > PAGE_SIZE)
3857 				break;
3858 			sprintf(&buf[index], "%08ld ", bucket_value);
3859 			index = strlen(buf);
3860 		}
3861 	}
3862 	sprintf(&buf[index], "\n");
3863 	return strlen(buf);
3864 }
3865 
3866 /*
3867  * Sysfs attribute to control the statistical data collection.
3868  */
3869 static DEVICE_ATTR(lpfc_stat_data_ctrl, S_IRUGO | S_IWUSR,
3870 		   lpfc_stat_data_ctrl_show, lpfc_stat_data_ctrl_store);
3871 
3872 /*
3873  * lpfc_drvr_stat_data: sysfs attr to get driver statistical data.
3874  */
3875 
3876 /*
3877  * Each Bucket takes 11 characters and 1 new line + 17 bytes WWN
3878  * for each target.
3879  */
3880 #define STAT_DATA_SIZE_PER_TARGET(NUM_BUCKETS) ((NUM_BUCKETS) * 11 + 18)
3881 #define MAX_STAT_DATA_SIZE_PER_TARGET \
3882 	STAT_DATA_SIZE_PER_TARGET(LPFC_MAX_BUCKET_COUNT)
3883 
3884 
3885 /**
3886  * sysfs_drvr_stat_data_read - Read function for lpfc_drvr_stat_data attribute
3887  * @filp: sysfs file
3888  * @kobj: Pointer to the kernel object
3889  * @bin_attr: Attribute object
3890  * @buff: Buffer pointer
3891  * @off: File offset
3892  * @count: Buffer size
3893  *
3894  * This function is the read call back function for lpfc_drvr_stat_data
3895  * sysfs file. This function export the statistical data to user
3896  * applications.
3897  **/
3898 static ssize_t
3899 sysfs_drvr_stat_data_read(struct file *filp, struct kobject *kobj,
3900 		struct bin_attribute *bin_attr,
3901 		char *buf, loff_t off, size_t count)
3902 {
3903 	struct device *dev = container_of(kobj, struct device,
3904 		kobj);
3905 	struct Scsi_Host  *shost = class_to_shost(dev);
3906 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3907 	struct lpfc_hba   *phba = vport->phba;
3908 	int i = 0, index = 0;
3909 	unsigned long nport_index;
3910 	struct lpfc_nodelist *ndlp = NULL;
3911 	nport_index = (unsigned long)off /
3912 		MAX_STAT_DATA_SIZE_PER_TARGET;
3913 
3914 	if (!vport->stat_data_enabled || vport->stat_data_blocked
3915 		|| (phba->bucket_type == LPFC_NO_BUCKET))
3916 		return 0;
3917 
3918 	spin_lock_irq(shost->host_lock);
3919 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
3920 		if (!NLP_CHK_NODE_ACT(ndlp) || !ndlp->lat_data)
3921 			continue;
3922 
3923 		if (nport_index > 0) {
3924 			nport_index--;
3925 			continue;
3926 		}
3927 
3928 		if ((index + MAX_STAT_DATA_SIZE_PER_TARGET)
3929 			> count)
3930 			break;
3931 
3932 		if (!ndlp->lat_data)
3933 			continue;
3934 
3935 		/* Print the WWN */
3936 		sprintf(&buf[index], "%02x%02x%02x%02x%02x%02x%02x%02x:",
3937 			ndlp->nlp_portname.u.wwn[0],
3938 			ndlp->nlp_portname.u.wwn[1],
3939 			ndlp->nlp_portname.u.wwn[2],
3940 			ndlp->nlp_portname.u.wwn[3],
3941 			ndlp->nlp_portname.u.wwn[4],
3942 			ndlp->nlp_portname.u.wwn[5],
3943 			ndlp->nlp_portname.u.wwn[6],
3944 			ndlp->nlp_portname.u.wwn[7]);
3945 
3946 		index = strlen(buf);
3947 
3948 		for (i = 0; i < LPFC_MAX_BUCKET_COUNT; i++) {
3949 			sprintf(&buf[index], "%010u,",
3950 				ndlp->lat_data[i].cmd_count);
3951 			index = strlen(buf);
3952 		}
3953 		sprintf(&buf[index], "\n");
3954 		index = strlen(buf);
3955 	}
3956 	spin_unlock_irq(shost->host_lock);
3957 	return index;
3958 }
3959 
3960 static struct bin_attribute sysfs_drvr_stat_data_attr = {
3961 	.attr = {
3962 		.name = "lpfc_drvr_stat_data",
3963 		.mode = S_IRUSR,
3964 	},
3965 	.size = LPFC_MAX_TARGET * MAX_STAT_DATA_SIZE_PER_TARGET,
3966 	.read = sysfs_drvr_stat_data_read,
3967 	.write = NULL,
3968 };
3969 
3970 /*
3971 # lpfc_link_speed: Link speed selection for initializing the Fibre Channel
3972 # connection.
3973 # Value range is [0,16]. Default value is 0.
3974 */
3975 /**
3976  * lpfc_link_speed_set - Set the adapters link speed
3977  * @phba: lpfc_hba pointer.
3978  * @val: link speed value.
3979  *
3980  * Description:
3981  * If val is in a valid range then set the adapter's link speed field and
3982  * issue a lip; if the lip fails reset the link speed to the old value.
3983  *
3984  * Notes:
3985  * If the value is not in range log a kernel error message and return an error.
3986  *
3987  * Returns:
3988  * zero if val is in range and lip okay.
3989  * non-zero return value from lpfc_issue_lip()
3990  * -EINVAL val out of range
3991  **/
3992 static ssize_t
3993 lpfc_link_speed_store(struct device *dev, struct device_attribute *attr,
3994 		const char *buf, size_t count)
3995 {
3996 	struct Scsi_Host  *shost = class_to_shost(dev);
3997 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3998 	struct lpfc_hba   *phba = vport->phba;
3999 	int val = LPFC_USER_LINK_SPEED_AUTO;
4000 	int nolip = 0;
4001 	const char *val_buf = buf;
4002 	int err;
4003 	uint32_t prev_val, if_type;
4004 
4005 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
4006 	if (if_type == LPFC_SLI_INTF_IF_TYPE_2 &&
4007 	    phba->hba_flag & HBA_FORCED_LINK_SPEED)
4008 		return -EPERM;
4009 
4010 	if (!strncmp(buf, "nolip ", strlen("nolip "))) {
4011 		nolip = 1;
4012 		val_buf = &buf[strlen("nolip ")];
4013 	}
4014 
4015 	if (!isdigit(val_buf[0]))
4016 		return -EINVAL;
4017 	if (sscanf(val_buf, "%i", &val) != 1)
4018 		return -EINVAL;
4019 
4020 	lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4021 		"3055 lpfc_link_speed changed from %d to %d %s\n",
4022 		phba->cfg_link_speed, val, nolip ? "(nolip)" : "(lip)");
4023 
4024 	if (((val == LPFC_USER_LINK_SPEED_1G) && !(phba->lmt & LMT_1Gb)) ||
4025 	    ((val == LPFC_USER_LINK_SPEED_2G) && !(phba->lmt & LMT_2Gb)) ||
4026 	    ((val == LPFC_USER_LINK_SPEED_4G) && !(phba->lmt & LMT_4Gb)) ||
4027 	    ((val == LPFC_USER_LINK_SPEED_8G) && !(phba->lmt & LMT_8Gb)) ||
4028 	    ((val == LPFC_USER_LINK_SPEED_10G) && !(phba->lmt & LMT_10Gb)) ||
4029 	    ((val == LPFC_USER_LINK_SPEED_16G) && !(phba->lmt & LMT_16Gb)) ||
4030 	    ((val == LPFC_USER_LINK_SPEED_32G) && !(phba->lmt & LMT_32Gb))) {
4031 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4032 				"2879 lpfc_link_speed attribute cannot be set "
4033 				"to %d. Speed is not supported by this port.\n",
4034 				val);
4035 		return -EINVAL;
4036 	}
4037 	if (val == LPFC_USER_LINK_SPEED_16G &&
4038 		 phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
4039 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4040 				"3112 lpfc_link_speed attribute cannot be set "
4041 				"to %d. Speed is not supported in loop mode.\n",
4042 				val);
4043 		return -EINVAL;
4044 	}
4045 	if ((val >= 0) && (val <= LPFC_USER_LINK_SPEED_MAX) &&
4046 	    (LPFC_USER_LINK_SPEED_BITMAP & (1 << val))) {
4047 		prev_val = phba->cfg_link_speed;
4048 		phba->cfg_link_speed = val;
4049 		if (nolip)
4050 			return strlen(buf);
4051 
4052 		err = lpfc_issue_lip(lpfc_shost_from_vport(phba->pport));
4053 		if (err) {
4054 			phba->cfg_link_speed = prev_val;
4055 			return -EINVAL;
4056 		} else
4057 			return strlen(buf);
4058 	}
4059 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4060 		"0469 lpfc_link_speed attribute cannot be set to %d, "
4061 		"allowed values are ["LPFC_LINK_SPEED_STRING"]\n", val);
4062 	return -EINVAL;
4063 }
4064 
4065 static int lpfc_link_speed = 0;
4066 module_param(lpfc_link_speed, int, S_IRUGO);
4067 MODULE_PARM_DESC(lpfc_link_speed, "Select link speed");
4068 lpfc_param_show(link_speed)
4069 
4070 /**
4071  * lpfc_link_speed_init - Set the adapters link speed
4072  * @phba: lpfc_hba pointer.
4073  * @val: link speed value.
4074  *
4075  * Description:
4076  * If val is in a valid range then set the adapter's link speed field.
4077  *
4078  * Notes:
4079  * If the value is not in range log a kernel error message, clear the link
4080  * speed and return an error.
4081  *
4082  * Returns:
4083  * zero if val saved.
4084  * -EINVAL val out of range
4085  **/
4086 static int
4087 lpfc_link_speed_init(struct lpfc_hba *phba, int val)
4088 {
4089 	if (val == LPFC_USER_LINK_SPEED_16G && phba->cfg_topology == 4) {
4090 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4091 			"3111 lpfc_link_speed of %d cannot "
4092 			"support loop mode, setting topology to default.\n",
4093 			 val);
4094 		phba->cfg_topology = 0;
4095 	}
4096 	if ((val >= 0) && (val <= LPFC_USER_LINK_SPEED_MAX) &&
4097 	    (LPFC_USER_LINK_SPEED_BITMAP & (1 << val))) {
4098 		phba->cfg_link_speed = val;
4099 		return 0;
4100 	}
4101 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4102 			"0405 lpfc_link_speed attribute cannot "
4103 			"be set to %d, allowed values are "
4104 			"["LPFC_LINK_SPEED_STRING"]\n", val);
4105 	phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
4106 	return -EINVAL;
4107 }
4108 
4109 static DEVICE_ATTR(lpfc_link_speed, S_IRUGO | S_IWUSR,
4110 		   lpfc_link_speed_show, lpfc_link_speed_store);
4111 
4112 /*
4113 # lpfc_aer_support: Support PCIe device Advanced Error Reporting (AER)
4114 #       0  = aer disabled or not supported
4115 #       1  = aer supported and enabled (default)
4116 # Value range is [0,1]. Default value is 1.
4117 */
4118 LPFC_ATTR(aer_support, 1, 0, 1,
4119 	"Enable PCIe device AER support");
4120 lpfc_param_show(aer_support)
4121 
4122 /**
4123  * lpfc_aer_support_store - Set the adapter for aer support
4124  *
4125  * @dev: class device that is converted into a Scsi_host.
4126  * @attr: device attribute, not used.
4127  * @buf: containing enable or disable aer flag.
4128  * @count: unused variable.
4129  *
4130  * Description:
4131  * If the val is 1 and currently the device's AER capability was not
4132  * enabled, invoke the kernel's enable AER helper routine, trying to
4133  * enable the device's AER capability. If the helper routine enabling
4134  * AER returns success, update the device's cfg_aer_support flag to
4135  * indicate AER is supported by the device; otherwise, if the device
4136  * AER capability is already enabled to support AER, then do nothing.
4137  *
4138  * If the val is 0 and currently the device's AER support was enabled,
4139  * invoke the kernel's disable AER helper routine. After that, update
4140  * the device's cfg_aer_support flag to indicate AER is not supported
4141  * by the device; otherwise, if the device AER capability is already
4142  * disabled from supporting AER, then do nothing.
4143  *
4144  * Returns:
4145  * length of the buf on success if val is in range the intended mode
4146  * is supported.
4147  * -EINVAL if val out of range or intended mode is not supported.
4148  **/
4149 static ssize_t
4150 lpfc_aer_support_store(struct device *dev, struct device_attribute *attr,
4151 		       const char *buf, size_t count)
4152 {
4153 	struct Scsi_Host *shost = class_to_shost(dev);
4154 	struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
4155 	struct lpfc_hba *phba = vport->phba;
4156 	int val = 0, rc = -EINVAL;
4157 
4158 	if (!isdigit(buf[0]))
4159 		return -EINVAL;
4160 	if (sscanf(buf, "%i", &val) != 1)
4161 		return -EINVAL;
4162 
4163 	switch (val) {
4164 	case 0:
4165 		if (phba->hba_flag & HBA_AER_ENABLED) {
4166 			rc = pci_disable_pcie_error_reporting(phba->pcidev);
4167 			if (!rc) {
4168 				spin_lock_irq(&phba->hbalock);
4169 				phba->hba_flag &= ~HBA_AER_ENABLED;
4170 				spin_unlock_irq(&phba->hbalock);
4171 				phba->cfg_aer_support = 0;
4172 				rc = strlen(buf);
4173 			} else
4174 				rc = -EPERM;
4175 		} else {
4176 			phba->cfg_aer_support = 0;
4177 			rc = strlen(buf);
4178 		}
4179 		break;
4180 	case 1:
4181 		if (!(phba->hba_flag & HBA_AER_ENABLED)) {
4182 			rc = pci_enable_pcie_error_reporting(phba->pcidev);
4183 			if (!rc) {
4184 				spin_lock_irq(&phba->hbalock);
4185 				phba->hba_flag |= HBA_AER_ENABLED;
4186 				spin_unlock_irq(&phba->hbalock);
4187 				phba->cfg_aer_support = 1;
4188 				rc = strlen(buf);
4189 			} else
4190 				 rc = -EPERM;
4191 		} else {
4192 			phba->cfg_aer_support = 1;
4193 			rc = strlen(buf);
4194 		}
4195 		break;
4196 	default:
4197 		rc = -EINVAL;
4198 		break;
4199 	}
4200 	return rc;
4201 }
4202 
4203 static DEVICE_ATTR(lpfc_aer_support, S_IRUGO | S_IWUSR,
4204 		   lpfc_aer_support_show, lpfc_aer_support_store);
4205 
4206 /**
4207  * lpfc_aer_cleanup_state - Clean up aer state to the aer enabled device
4208  * @dev: class device that is converted into a Scsi_host.
4209  * @attr: device attribute, not used.
4210  * @buf: containing flag 1 for aer cleanup state.
4211  * @count: unused variable.
4212  *
4213  * Description:
4214  * If the @buf contains 1 and the device currently has the AER support
4215  * enabled, then invokes the kernel AER helper routine
4216  * pci_cleanup_aer_uncorrect_error_status to clean up the uncorrectable
4217  * error status register.
4218  *
4219  * Notes:
4220  *
4221  * Returns:
4222  * -EINVAL if the buf does not contain the 1 or the device is not currently
4223  * enabled with the AER support.
4224  **/
4225 static ssize_t
4226 lpfc_aer_cleanup_state(struct device *dev, struct device_attribute *attr,
4227 		       const char *buf, size_t count)
4228 {
4229 	struct Scsi_Host  *shost = class_to_shost(dev);
4230 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4231 	struct lpfc_hba   *phba = vport->phba;
4232 	int val, rc = -1;
4233 
4234 	if (!isdigit(buf[0]))
4235 		return -EINVAL;
4236 	if (sscanf(buf, "%i", &val) != 1)
4237 		return -EINVAL;
4238 	if (val != 1)
4239 		return -EINVAL;
4240 
4241 	if (phba->hba_flag & HBA_AER_ENABLED)
4242 		rc = pci_cleanup_aer_uncorrect_error_status(phba->pcidev);
4243 
4244 	if (rc == 0)
4245 		return strlen(buf);
4246 	else
4247 		return -EPERM;
4248 }
4249 
4250 static DEVICE_ATTR(lpfc_aer_state_cleanup, S_IWUSR, NULL,
4251 		   lpfc_aer_cleanup_state);
4252 
4253 /**
4254  * lpfc_sriov_nr_virtfn_store - Enable the adapter for sr-iov virtual functions
4255  *
4256  * @dev: class device that is converted into a Scsi_host.
4257  * @attr: device attribute, not used.
4258  * @buf: containing the string the number of vfs to be enabled.
4259  * @count: unused variable.
4260  *
4261  * Description:
4262  * When this api is called either through user sysfs, the driver shall
4263  * try to enable or disable SR-IOV virtual functions according to the
4264  * following:
4265  *
4266  * If zero virtual function has been enabled to the physical function,
4267  * the driver shall invoke the pci enable virtual function api trying
4268  * to enable the virtual functions. If the nr_vfn provided is greater
4269  * than the maximum supported, the maximum virtual function number will
4270  * be used for invoking the api; otherwise, the nr_vfn provided shall
4271  * be used for invoking the api. If the api call returned success, the
4272  * actual number of virtual functions enabled will be set to the driver
4273  * cfg_sriov_nr_virtfn; otherwise, -EINVAL shall be returned and driver
4274  * cfg_sriov_nr_virtfn remains zero.
4275  *
4276  * If none-zero virtual functions have already been enabled to the
4277  * physical function, as reflected by the driver's cfg_sriov_nr_virtfn,
4278  * -EINVAL will be returned and the driver does nothing;
4279  *
4280  * If the nr_vfn provided is zero and none-zero virtual functions have
4281  * been enabled, as indicated by the driver's cfg_sriov_nr_virtfn, the
4282  * disabling virtual function api shall be invoded to disable all the
4283  * virtual functions and driver's cfg_sriov_nr_virtfn shall be set to
4284  * zero. Otherwise, if zero virtual function has been enabled, do
4285  * nothing.
4286  *
4287  * Returns:
4288  * length of the buf on success if val is in range the intended mode
4289  * is supported.
4290  * -EINVAL if val out of range or intended mode is not supported.
4291  **/
4292 static ssize_t
4293 lpfc_sriov_nr_virtfn_store(struct device *dev, struct device_attribute *attr,
4294 			 const char *buf, size_t count)
4295 {
4296 	struct Scsi_Host *shost = class_to_shost(dev);
4297 	struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
4298 	struct lpfc_hba *phba = vport->phba;
4299 	struct pci_dev *pdev = phba->pcidev;
4300 	int val = 0, rc = -EINVAL;
4301 
4302 	/* Sanity check on user data */
4303 	if (!isdigit(buf[0]))
4304 		return -EINVAL;
4305 	if (sscanf(buf, "%i", &val) != 1)
4306 		return -EINVAL;
4307 	if (val < 0)
4308 		return -EINVAL;
4309 
4310 	/* Request disabling virtual functions */
4311 	if (val == 0) {
4312 		if (phba->cfg_sriov_nr_virtfn > 0) {
4313 			pci_disable_sriov(pdev);
4314 			phba->cfg_sriov_nr_virtfn = 0;
4315 		}
4316 		return strlen(buf);
4317 	}
4318 
4319 	/* Request enabling virtual functions */
4320 	if (phba->cfg_sriov_nr_virtfn > 0) {
4321 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4322 				"3018 There are %d virtual functions "
4323 				"enabled on physical function.\n",
4324 				phba->cfg_sriov_nr_virtfn);
4325 		return -EEXIST;
4326 	}
4327 
4328 	if (val <= LPFC_MAX_VFN_PER_PFN)
4329 		phba->cfg_sriov_nr_virtfn = val;
4330 	else {
4331 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4332 				"3019 Enabling %d virtual functions is not "
4333 				"allowed.\n", val);
4334 		return -EINVAL;
4335 	}
4336 
4337 	rc = lpfc_sli_probe_sriov_nr_virtfn(phba, phba->cfg_sriov_nr_virtfn);
4338 	if (rc) {
4339 		phba->cfg_sriov_nr_virtfn = 0;
4340 		rc = -EPERM;
4341 	} else
4342 		rc = strlen(buf);
4343 
4344 	return rc;
4345 }
4346 
4347 LPFC_ATTR(sriov_nr_virtfn, LPFC_DEF_VFN_PER_PFN, 0, LPFC_MAX_VFN_PER_PFN,
4348 	"Enable PCIe device SR-IOV virtual fn");
4349 
4350 lpfc_param_show(sriov_nr_virtfn)
4351 static DEVICE_ATTR(lpfc_sriov_nr_virtfn, S_IRUGO | S_IWUSR,
4352 		   lpfc_sriov_nr_virtfn_show, lpfc_sriov_nr_virtfn_store);
4353 
4354 /**
4355  * lpfc_request_firmware_store - Request for Linux generic firmware upgrade
4356  *
4357  * @dev: class device that is converted into a Scsi_host.
4358  * @attr: device attribute, not used.
4359  * @buf: containing the string the number of vfs to be enabled.
4360  * @count: unused variable.
4361  *
4362  * Description:
4363  *
4364  * Returns:
4365  * length of the buf on success if val is in range the intended mode
4366  * is supported.
4367  * -EINVAL if val out of range or intended mode is not supported.
4368  **/
4369 static ssize_t
4370 lpfc_request_firmware_upgrade_store(struct device *dev,
4371 				    struct device_attribute *attr,
4372 				    const char *buf, size_t count)
4373 {
4374 	struct Scsi_Host *shost = class_to_shost(dev);
4375 	struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
4376 	struct lpfc_hba *phba = vport->phba;
4377 	int val = 0, rc = -EINVAL;
4378 
4379 	/* Sanity check on user data */
4380 	if (!isdigit(buf[0]))
4381 		return -EINVAL;
4382 	if (sscanf(buf, "%i", &val) != 1)
4383 		return -EINVAL;
4384 	if (val != 1)
4385 		return -EINVAL;
4386 
4387 	rc = lpfc_sli4_request_firmware_update(phba, RUN_FW_UPGRADE);
4388 	if (rc)
4389 		rc = -EPERM;
4390 	else
4391 		rc = strlen(buf);
4392 	return rc;
4393 }
4394 
4395 static int lpfc_req_fw_upgrade;
4396 module_param(lpfc_req_fw_upgrade, int, S_IRUGO|S_IWUSR);
4397 MODULE_PARM_DESC(lpfc_req_fw_upgrade, "Enable Linux generic firmware upgrade");
4398 lpfc_param_show(request_firmware_upgrade)
4399 
4400 /**
4401  * lpfc_request_firmware_upgrade_init - Enable initial linux generic fw upgrade
4402  * @phba: lpfc_hba pointer.
4403  * @val: 0 or 1.
4404  *
4405  * Description:
4406  * Set the initial Linux generic firmware upgrade enable or disable flag.
4407  *
4408  * Returns:
4409  * zero if val saved.
4410  * -EINVAL val out of range
4411  **/
4412 static int
4413 lpfc_request_firmware_upgrade_init(struct lpfc_hba *phba, int val)
4414 {
4415 	if (val >= 0 && val <= 1) {
4416 		phba->cfg_request_firmware_upgrade = val;
4417 		return 0;
4418 	}
4419 	return -EINVAL;
4420 }
4421 static DEVICE_ATTR(lpfc_req_fw_upgrade, S_IRUGO | S_IWUSR,
4422 		   lpfc_request_firmware_upgrade_show,
4423 		   lpfc_request_firmware_upgrade_store);
4424 
4425 /**
4426  * lpfc_fcp_imax_store
4427  *
4428  * @dev: class device that is converted into a Scsi_host.
4429  * @attr: device attribute, not used.
4430  * @buf: string with the number of fast-path FCP interrupts per second.
4431  * @count: unused variable.
4432  *
4433  * Description:
4434  * If val is in a valid range [636,651042], then set the adapter's
4435  * maximum number of fast-path FCP interrupts per second.
4436  *
4437  * Returns:
4438  * length of the buf on success if val is in range the intended mode
4439  * is supported.
4440  * -EINVAL if val out of range or intended mode is not supported.
4441  **/
4442 static ssize_t
4443 lpfc_fcp_imax_store(struct device *dev, struct device_attribute *attr,
4444 			 const char *buf, size_t count)
4445 {
4446 	struct Scsi_Host *shost = class_to_shost(dev);
4447 	struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
4448 	struct lpfc_hba *phba = vport->phba;
4449 	int val = 0, i;
4450 
4451 	/* fcp_imax is only valid for SLI4 */
4452 	if (phba->sli_rev != LPFC_SLI_REV4)
4453 		return -EINVAL;
4454 
4455 	/* Sanity check on user data */
4456 	if (!isdigit(buf[0]))
4457 		return -EINVAL;
4458 	if (sscanf(buf, "%i", &val) != 1)
4459 		return -EINVAL;
4460 
4461 	/*
4462 	 * Value range for the HBA is [5000,5000000]
4463 	 * The value for each EQ depends on how many EQs are configured.
4464 	 * Allow value == 0
4465 	 */
4466 	if (val && (val < LPFC_MIN_IMAX || val > LPFC_MAX_IMAX))
4467 		return -EINVAL;
4468 
4469 	phba->cfg_fcp_imax = (uint32_t)val;
4470 
4471 	for (i = 0; i < phba->io_channel_irqs; i += LPFC_MAX_EQ_DELAY_EQID_CNT)
4472 		lpfc_modify_hba_eq_delay(phba, i);
4473 
4474 	return strlen(buf);
4475 }
4476 
4477 /*
4478 # lpfc_fcp_imax: The maximum number of fast-path FCP interrupts per second
4479 # for the HBA.
4480 #
4481 # Value range is [5,000 to 5,000,000]. Default value is 50,000.
4482 */
4483 static int lpfc_fcp_imax = LPFC_DEF_IMAX;
4484 module_param(lpfc_fcp_imax, int, S_IRUGO|S_IWUSR);
4485 MODULE_PARM_DESC(lpfc_fcp_imax,
4486 	    "Set the maximum number of FCP interrupts per second per HBA");
4487 lpfc_param_show(fcp_imax)
4488 
4489 /**
4490  * lpfc_fcp_imax_init - Set the initial sr-iov virtual function enable
4491  * @phba: lpfc_hba pointer.
4492  * @val: link speed value.
4493  *
4494  * Description:
4495  * If val is in a valid range [636,651042], then initialize the adapter's
4496  * maximum number of fast-path FCP interrupts per second.
4497  *
4498  * Returns:
4499  * zero if val saved.
4500  * -EINVAL val out of range
4501  **/
4502 static int
4503 lpfc_fcp_imax_init(struct lpfc_hba *phba, int val)
4504 {
4505 	if (phba->sli_rev != LPFC_SLI_REV4) {
4506 		phba->cfg_fcp_imax = 0;
4507 		return 0;
4508 	}
4509 
4510 	if ((val >= LPFC_MIN_IMAX && val <= LPFC_MAX_IMAX) ||
4511 	    (val == 0)) {
4512 		phba->cfg_fcp_imax = val;
4513 		return 0;
4514 	}
4515 
4516 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4517 			"3016 lpfc_fcp_imax: %d out of range, using default\n",
4518 			val);
4519 	phba->cfg_fcp_imax = LPFC_DEF_IMAX;
4520 
4521 	return 0;
4522 }
4523 
4524 static DEVICE_ATTR(lpfc_fcp_imax, S_IRUGO | S_IWUSR,
4525 		   lpfc_fcp_imax_show, lpfc_fcp_imax_store);
4526 
4527 /**
4528  * lpfc_state_show - Display current driver CPU affinity
4529  * @dev: class converted to a Scsi_host structure.
4530  * @attr: device attribute, not used.
4531  * @buf: on return contains text describing the state of the link.
4532  *
4533  * Returns: size of formatted string.
4534  **/
4535 static ssize_t
4536 lpfc_fcp_cpu_map_show(struct device *dev, struct device_attribute *attr,
4537 		      char *buf)
4538 {
4539 	struct Scsi_Host  *shost = class_to_shost(dev);
4540 	struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
4541 	struct lpfc_hba   *phba = vport->phba;
4542 	struct lpfc_vector_map_info *cpup;
4543 	int  len = 0;
4544 
4545 	if ((phba->sli_rev != LPFC_SLI_REV4) ||
4546 	    (phba->intr_type != MSIX))
4547 		return len;
4548 
4549 	switch (phba->cfg_fcp_cpu_map) {
4550 	case 0:
4551 		len += snprintf(buf + len, PAGE_SIZE-len,
4552 				"fcp_cpu_map: No mapping (%d)\n",
4553 				phba->cfg_fcp_cpu_map);
4554 		return len;
4555 	case 1:
4556 		len += snprintf(buf + len, PAGE_SIZE-len,
4557 				"fcp_cpu_map: HBA centric mapping (%d): "
4558 				"%d online CPUs\n",
4559 				phba->cfg_fcp_cpu_map,
4560 				phba->sli4_hba.num_online_cpu);
4561 		break;
4562 	case 2:
4563 		len += snprintf(buf + len, PAGE_SIZE-len,
4564 				"fcp_cpu_map: Driver centric mapping (%d): "
4565 				"%d online CPUs\n",
4566 				phba->cfg_fcp_cpu_map,
4567 				phba->sli4_hba.num_online_cpu);
4568 		break;
4569 	}
4570 
4571 	while (phba->sli4_hba.curr_disp_cpu < phba->sli4_hba.num_present_cpu) {
4572 		cpup = &phba->sli4_hba.cpu_map[phba->sli4_hba.curr_disp_cpu];
4573 
4574 		/* margin should fit in this and the truncated message */
4575 		if (cpup->irq == LPFC_VECTOR_MAP_EMPTY)
4576 			len += snprintf(buf + len, PAGE_SIZE-len,
4577 					"CPU %02d io_chan %02d "
4578 					"physid %d coreid %d\n",
4579 					phba->sli4_hba.curr_disp_cpu,
4580 					cpup->channel_id, cpup->phys_id,
4581 					cpup->core_id);
4582 		else
4583 			len += snprintf(buf + len, PAGE_SIZE-len,
4584 					"CPU %02d io_chan %02d "
4585 					"physid %d coreid %d IRQ %d\n",
4586 					phba->sli4_hba.curr_disp_cpu,
4587 					cpup->channel_id, cpup->phys_id,
4588 					cpup->core_id, cpup->irq);
4589 
4590 		phba->sli4_hba.curr_disp_cpu++;
4591 
4592 		/* display max number of CPUs keeping some margin */
4593 		if (phba->sli4_hba.curr_disp_cpu <
4594 				phba->sli4_hba.num_present_cpu &&
4595 				(len >= (PAGE_SIZE - 64))) {
4596 			len += snprintf(buf + len, PAGE_SIZE-len, "more...\n");
4597 			break;
4598 		}
4599 	}
4600 
4601 	if (phba->sli4_hba.curr_disp_cpu == phba->sli4_hba.num_present_cpu)
4602 		phba->sli4_hba.curr_disp_cpu = 0;
4603 
4604 	return len;
4605 }
4606 
4607 /**
4608  * lpfc_fcp_cpu_map_store - Change CPU affinity of driver vectors
4609  * @dev: class device that is converted into a Scsi_host.
4610  * @attr: device attribute, not used.
4611  * @buf: one or more lpfc_polling_flags values.
4612  * @count: not used.
4613  *
4614  * Returns:
4615  * -EINVAL  - Not implemented yet.
4616  **/
4617 static ssize_t
4618 lpfc_fcp_cpu_map_store(struct device *dev, struct device_attribute *attr,
4619 		       const char *buf, size_t count)
4620 {
4621 	int status = -EINVAL;
4622 	return status;
4623 }
4624 
4625 /*
4626 # lpfc_fcp_cpu_map: Defines how to map CPUs to IRQ vectors
4627 # for the HBA.
4628 #
4629 # Value range is [0 to 2]. Default value is LPFC_DRIVER_CPU_MAP (2).
4630 #	0 - Do not affinitze IRQ vectors
4631 #	1 - Affintize HBA vectors with respect to each HBA
4632 #	    (start with CPU0 for each HBA)
4633 #	2 - Affintize HBA vectors with respect to the entire driver
4634 #	    (round robin thru all CPUs across all HBAs)
4635 */
4636 static int lpfc_fcp_cpu_map = LPFC_DRIVER_CPU_MAP;
4637 module_param(lpfc_fcp_cpu_map, int, S_IRUGO|S_IWUSR);
4638 MODULE_PARM_DESC(lpfc_fcp_cpu_map,
4639 		 "Defines how to map CPUs to IRQ vectors per HBA");
4640 
4641 /**
4642  * lpfc_fcp_cpu_map_init - Set the initial sr-iov virtual function enable
4643  * @phba: lpfc_hba pointer.
4644  * @val: link speed value.
4645  *
4646  * Description:
4647  * If val is in a valid range [0-2], then affinitze the adapter's
4648  * MSIX vectors.
4649  *
4650  * Returns:
4651  * zero if val saved.
4652  * -EINVAL val out of range
4653  **/
4654 static int
4655 lpfc_fcp_cpu_map_init(struct lpfc_hba *phba, int val)
4656 {
4657 	if (phba->sli_rev != LPFC_SLI_REV4) {
4658 		phba->cfg_fcp_cpu_map = 0;
4659 		return 0;
4660 	}
4661 
4662 	if (val >= LPFC_MIN_CPU_MAP && val <= LPFC_MAX_CPU_MAP) {
4663 		phba->cfg_fcp_cpu_map = val;
4664 		return 0;
4665 	}
4666 
4667 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4668 			"3326 lpfc_fcp_cpu_map: %d out of range, using "
4669 			"default\n", val);
4670 	phba->cfg_fcp_cpu_map = LPFC_DRIVER_CPU_MAP;
4671 
4672 	return 0;
4673 }
4674 
4675 static DEVICE_ATTR(lpfc_fcp_cpu_map, S_IRUGO | S_IWUSR,
4676 		   lpfc_fcp_cpu_map_show, lpfc_fcp_cpu_map_store);
4677 
4678 /*
4679 # lpfc_fcp_class:  Determines FC class to use for the FCP protocol.
4680 # Value range is [2,3]. Default value is 3.
4681 */
4682 LPFC_VPORT_ATTR_R(fcp_class, 3, 2, 3,
4683 		  "Select Fibre Channel class of service for FCP sequences");
4684 
4685 /*
4686 # lpfc_use_adisc: Use ADISC for FCP rediscovery instead of PLOGI. Value range
4687 # is [0,1]. Default value is 0.
4688 */
4689 LPFC_VPORT_ATTR_RW(use_adisc, 0, 0, 1,
4690 		   "Use ADISC on rediscovery to authenticate FCP devices");
4691 
4692 /*
4693 # lpfc_first_burst_size: First burst size to use on the NPorts
4694 # that support first burst.
4695 # Value range is [0,65536]. Default value is 0.
4696 */
4697 LPFC_VPORT_ATTR_RW(first_burst_size, 0, 0, 65536,
4698 		   "First burst size for Targets that support first burst");
4699 
4700 /*
4701 * lpfc_nvmet_fb_size: NVME Target mode supported first burst size.
4702 * When the driver is configured as an NVME target, this value is
4703 * communicated to the NVME initiator in the PRLI response.  It is
4704 * used only when the lpfc_nvme_enable_fb and lpfc_nvmet_support
4705 * parameters are set and the target is sending the PRLI RSP.
4706 * Parameter supported on physical port only - no NPIV support.
4707 * Value range is [0,65536]. Default value is 0.
4708 */
4709 LPFC_ATTR_RW(nvmet_fb_size, 0, 0, 65536,
4710 	     "NVME Target mode first burst size in 512B increments.");
4711 
4712 /*
4713  * lpfc_nvme_enable_fb: Enable NVME first burst on I and T functions.
4714  * For the Initiator (I), enabling this parameter means that an NVMET
4715  * PRLI response with FBA enabled and an FB_SIZE set to a nonzero value will be
4716  * processed by the initiator for subsequent NVME FCP IO. For the target
4717  * function (T), enabling this parameter qualifies the lpfc_nvmet_fb_size
4718  * driver parameter as the target function's first burst size returned to the
4719  * initiator in the target's NVME PRLI response. Parameter supported on physical
4720  * port only - no NPIV support.
4721  * Value range is [0,1]. Default value is 0 (disabled).
4722  */
4723 LPFC_ATTR_RW(nvme_enable_fb, 0, 0, 1,
4724 	     "Enable First Burst feature on I and T functions.");
4725 
4726 /*
4727 # lpfc_max_scsicmpl_time: Use scsi command completion time to control I/O queue
4728 # depth. Default value is 0. When the value of this parameter is zero the
4729 # SCSI command completion time is not used for controlling I/O queue depth. When
4730 # the parameter is set to a non-zero value, the I/O queue depth is controlled
4731 # to limit the I/O completion time to the parameter value.
4732 # The value is set in milliseconds.
4733 */
4734 LPFC_VPORT_ATTR(max_scsicmpl_time, 0, 0, 60000,
4735 	"Use command completion time to control queue depth");
4736 
4737 lpfc_vport_param_show(max_scsicmpl_time);
4738 static int
4739 lpfc_max_scsicmpl_time_set(struct lpfc_vport *vport, int val)
4740 {
4741 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4742 	struct lpfc_nodelist *ndlp, *next_ndlp;
4743 
4744 	if (val == vport->cfg_max_scsicmpl_time)
4745 		return 0;
4746 	if ((val < 0) || (val > 60000))
4747 		return -EINVAL;
4748 	vport->cfg_max_scsicmpl_time = val;
4749 
4750 	spin_lock_irq(shost->host_lock);
4751 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
4752 		if (!NLP_CHK_NODE_ACT(ndlp))
4753 			continue;
4754 		if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
4755 			continue;
4756 		ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
4757 	}
4758 	spin_unlock_irq(shost->host_lock);
4759 	return 0;
4760 }
4761 lpfc_vport_param_store(max_scsicmpl_time);
4762 static DEVICE_ATTR(lpfc_max_scsicmpl_time, S_IRUGO | S_IWUSR,
4763 		   lpfc_max_scsicmpl_time_show,
4764 		   lpfc_max_scsicmpl_time_store);
4765 
4766 /*
4767 # lpfc_ack0: Use ACK0, instead of ACK1 for class 2 acknowledgement. Value
4768 # range is [0,1]. Default value is 0.
4769 */
4770 LPFC_ATTR_R(ack0, 0, 0, 1, "Enable ACK0 support");
4771 
4772 /*
4773  * lpfc_io_sched: Determine scheduling algrithmn for issuing FCP cmds
4774  * range is [0,1]. Default value is 0.
4775  * For [0], FCP commands are issued to Work Queues ina round robin fashion.
4776  * For [1], FCP commands are issued to a Work Queue associated with the
4777  *          current CPU.
4778  *
4779  * LPFC_FCP_SCHED_ROUND_ROBIN == 0
4780  * LPFC_FCP_SCHED_BY_CPU == 1
4781  *
4782  * The driver dynamically sets this to 1 (BY_CPU) if it's able to set up cpu
4783  * affinity for FCP/NVME I/Os through Work Queues associated with the current
4784  * CPU. Otherwise, the default 0 (Round Robin) scheduling of FCP/NVME I/Os
4785  * through WQs will be used.
4786  */
4787 LPFC_ATTR_RW(fcp_io_sched, LPFC_FCP_SCHED_ROUND_ROBIN,
4788 	     LPFC_FCP_SCHED_ROUND_ROBIN,
4789 	     LPFC_FCP_SCHED_BY_CPU,
4790 	     "Determine scheduling algorithm for "
4791 	     "issuing commands [0] - Round Robin, [1] - Current CPU");
4792 
4793 /*
4794 # lpfc_fcp2_no_tgt_reset: Determine bus reset behavior
4795 # range is [0,1]. Default value is 0.
4796 # For [0], bus reset issues target reset to ALL devices
4797 # For [1], bus reset issues target reset to non-FCP2 devices
4798 */
4799 LPFC_ATTR_RW(fcp2_no_tgt_reset, 0, 0, 1, "Determine bus reset behavior for "
4800 	     "FCP2 devices [0] - issue tgt reset, [1] - no tgt reset");
4801 
4802 
4803 /*
4804 # lpfc_cr_delay & lpfc_cr_count: Default values for I/O colaesing
4805 # cr_delay (msec) or cr_count outstanding commands. cr_delay can take
4806 # value [0,63]. cr_count can take value [1,255]. Default value of cr_delay
4807 # is 0. Default value of cr_count is 1. The cr_count feature is disabled if
4808 # cr_delay is set to 0.
4809 */
4810 LPFC_ATTR_RW(cr_delay, 0, 0, 63, "A count of milliseconds after which an "
4811 		"interrupt response is generated");
4812 
4813 LPFC_ATTR_RW(cr_count, 1, 1, 255, "A count of I/O completions after which an "
4814 		"interrupt response is generated");
4815 
4816 /*
4817 # lpfc_multi_ring_support:  Determines how many rings to spread available
4818 # cmd/rsp IOCB entries across.
4819 # Value range is [1,2]. Default value is 1.
4820 */
4821 LPFC_ATTR_R(multi_ring_support, 1, 1, 2, "Determines number of primary "
4822 		"SLI rings to spread IOCB entries across");
4823 
4824 /*
4825 # lpfc_multi_ring_rctl:  If lpfc_multi_ring_support is enabled, this
4826 # identifies what rctl value to configure the additional ring for.
4827 # Value range is [1,0xff]. Default value is 4 (Unsolicated Data).
4828 */
4829 LPFC_ATTR_R(multi_ring_rctl, FC_RCTL_DD_UNSOL_DATA, 1,
4830 	     255, "Identifies RCTL for additional ring configuration");
4831 
4832 /*
4833 # lpfc_multi_ring_type:  If lpfc_multi_ring_support is enabled, this
4834 # identifies what type value to configure the additional ring for.
4835 # Value range is [1,0xff]. Default value is 5 (LLC/SNAP).
4836 */
4837 LPFC_ATTR_R(multi_ring_type, FC_TYPE_IP, 1,
4838 	     255, "Identifies TYPE for additional ring configuration");
4839 
4840 /*
4841 # lpfc_enable_SmartSAN: Sets up FDMI support for SmartSAN
4842 #       0  = SmartSAN functionality disabled (default)
4843 #       1  = SmartSAN functionality enabled
4844 # This parameter will override the value of lpfc_fdmi_on module parameter.
4845 # Value range is [0,1]. Default value is 0.
4846 */
4847 LPFC_ATTR_R(enable_SmartSAN, 0, 0, 1, "Enable SmartSAN functionality");
4848 
4849 /*
4850 # lpfc_fdmi_on: Controls FDMI support.
4851 #       0       No FDMI support (default)
4852 #       1       Traditional FDMI support
4853 # Traditional FDMI support means the driver will assume FDMI-2 support;
4854 # however, if that fails, it will fallback to FDMI-1.
4855 # If lpfc_enable_SmartSAN is set to 1, the driver ignores lpfc_fdmi_on.
4856 # If lpfc_enable_SmartSAN is set 0, the driver uses the current value of
4857 # lpfc_fdmi_on.
4858 # Value range [0,1]. Default value is 0.
4859 */
4860 LPFC_ATTR_R(fdmi_on, 0, 0, 1, "Enable FDMI support");
4861 
4862 /*
4863 # Specifies the maximum number of ELS cmds we can have outstanding (for
4864 # discovery). Value range is [1,64]. Default value = 32.
4865 */
4866 LPFC_VPORT_ATTR(discovery_threads, 32, 1, 64, "Maximum number of ELS commands "
4867 		 "during discovery");
4868 
4869 /*
4870 # lpfc_max_luns: maximum allowed LUN ID. This is the highest LUN ID that
4871 #    will be scanned by the SCSI midlayer when sequential scanning is
4872 #    used; and is also the highest LUN ID allowed when the SCSI midlayer
4873 #    parses REPORT_LUN responses. The lpfc driver has no LUN count or
4874 #    LUN ID limit, but the SCSI midlayer requires this field for the uses
4875 #    above. The lpfc driver limits the default value to 255 for two reasons.
4876 #    As it bounds the sequential scan loop, scanning for thousands of luns
4877 #    on a target can take minutes of wall clock time.  Additionally,
4878 #    there are FC targets, such as JBODs, that only recognize 8-bits of
4879 #    LUN ID. When they receive a value greater than 8 bits, they chop off
4880 #    the high order bits. In other words, they see LUN IDs 0, 256, 512,
4881 #    and so on all as LUN ID 0. This causes the linux kernel, which sees
4882 #    valid responses at each of the LUN IDs, to believe there are multiple
4883 #    devices present, when in fact, there is only 1.
4884 #    A customer that is aware of their target behaviors, and the results as
4885 #    indicated above, is welcome to increase the lpfc_max_luns value.
4886 #    As mentioned, this value is not used by the lpfc driver, only the
4887 #    SCSI midlayer.
4888 # Value range is [0,65535]. Default value is 255.
4889 # NOTE: The SCSI layer might probe all allowed LUN on some old targets.
4890 */
4891 LPFC_VPORT_ULL_ATTR_R(max_luns, 255, 0, 65535, "Maximum allowed LUN ID");
4892 
4893 /*
4894 # lpfc_poll_tmo: .Milliseconds driver will wait between polling FCP ring.
4895 # Value range is [1,255], default value is 10.
4896 */
4897 LPFC_ATTR_RW(poll_tmo, 10, 1, 255,
4898 	     "Milliseconds driver will wait between polling FCP ring");
4899 
4900 /*
4901 # lpfc_task_mgmt_tmo: Maximum time to wait for task management commands
4902 # to complete in seconds. Value range is [5,180], default value is 60.
4903 */
4904 LPFC_ATTR_RW(task_mgmt_tmo, 60, 5, 180,
4905 	     "Maximum time to wait for task management commands to complete");
4906 /*
4907 # lpfc_use_msi: Use MSI (Message Signaled Interrupts) in systems that
4908 #		support this feature
4909 #       0  = MSI disabled
4910 #       1  = MSI enabled
4911 #       2  = MSI-X enabled (default)
4912 # Value range is [0,2]. Default value is 2.
4913 */
4914 LPFC_ATTR_R(use_msi, 2, 0, 2, "Use Message Signaled Interrupts (1) or "
4915 	    "MSI-X (2), if possible");
4916 
4917 /*
4918  * lpfc_nvme_oas: Use the oas bit when sending NVME/NVMET IOs
4919  *
4920  *      0  = NVME OAS disabled
4921  *      1  = NVME OAS enabled
4922  *
4923  * Value range is [0,1]. Default value is 0.
4924  */
4925 LPFC_ATTR_RW(nvme_oas, 0, 0, 1,
4926 	     "Use OAS bit on NVME IOs");
4927 
4928 /*
4929  * lpfc_fcp_io_channel: Set the number of FCP IO channels the driver
4930  * will advertise it supports to the SCSI layer. This also will map to
4931  * the number of WQs the driver will create.
4932  *
4933  *      0    = Configure the number of io channels to the number of active CPUs.
4934  *      1,32 = Manually specify how many io channels to use.
4935  *
4936  * Value range is [0,32]. Default value is 4.
4937  */
4938 LPFC_ATTR_R(fcp_io_channel,
4939 	    LPFC_FCP_IO_CHAN_DEF,
4940 	    LPFC_HBA_IO_CHAN_MIN, LPFC_HBA_IO_CHAN_MAX,
4941 	    "Set the number of FCP I/O channels");
4942 
4943 /*
4944  * lpfc_nvme_io_channel: Set the number of IO hardware queues the driver
4945  * will advertise it supports to the NVME layer. This also will map to
4946  * the number of WQs the driver will create.
4947  *
4948  * This module parameter is valid when lpfc_enable_fc4_type is set
4949  * to support NVME.
4950  *
4951  * The NVME Layer will try to create this many, plus 1 administrative
4952  * hardware queue. The administrative queue will always map to WQ 0
4953  * A hardware IO queue maps (qidx) to a specific driver WQ.
4954  *
4955  *      0    = Configure the number of io channels to the number of active CPUs.
4956  *      1,32 = Manually specify how many io channels to use.
4957  *
4958  * Value range is [0,32]. Default value is 0.
4959  */
4960 LPFC_ATTR_R(nvme_io_channel,
4961 	    LPFC_NVME_IO_CHAN_DEF,
4962 	    LPFC_HBA_IO_CHAN_MIN, LPFC_HBA_IO_CHAN_MAX,
4963 	    "Set the number of NVME I/O channels");
4964 
4965 /*
4966 # lpfc_enable_hba_reset: Allow or prevent HBA resets to the hardware.
4967 #       0  = HBA resets disabled
4968 #       1  = HBA resets enabled (default)
4969 # Value range is [0,1]. Default value is 1.
4970 */
4971 LPFC_ATTR_R(enable_hba_reset, 1, 0, 1, "Enable HBA resets from the driver.");
4972 
4973 /*
4974 # lpfc_enable_hba_heartbeat: Disable HBA heartbeat timer..
4975 #       0  = HBA Heartbeat disabled
4976 #       1  = HBA Heartbeat enabled (default)
4977 # Value range is [0,1]. Default value is 1.
4978 */
4979 LPFC_ATTR_R(enable_hba_heartbeat, 0, 0, 1, "Enable HBA Heartbeat.");
4980 
4981 /*
4982 # lpfc_EnableXLane: Enable Express Lane Feature
4983 #      0x0   Express Lane Feature disabled
4984 #      0x1   Express Lane Feature enabled
4985 # Value range is [0,1]. Default value is 0.
4986 */
4987 LPFC_ATTR_R(EnableXLane, 0, 0, 1, "Enable Express Lane Feature.");
4988 
4989 /*
4990 # lpfc_XLanePriority:  Define CS_CTL priority for Express Lane Feature
4991 #       0x0 - 0x7f  = CS_CTL field in FC header (high 7 bits)
4992 # Value range is [0x0,0x7f]. Default value is 0
4993 */
4994 LPFC_ATTR_RW(XLanePriority, 0, 0x0, 0x7f, "CS_CTL for Express Lane Feature.");
4995 
4996 /*
4997 # lpfc_enable_bg: Enable BlockGuard (Emulex's Implementation of T10-DIF)
4998 #       0  = BlockGuard disabled (default)
4999 #       1  = BlockGuard enabled
5000 # Value range is [0,1]. Default value is 0.
5001 */
5002 LPFC_ATTR_R(enable_bg, 0, 0, 1, "Enable BlockGuard Support");
5003 
5004 /*
5005 # lpfc_fcp_look_ahead: Look ahead for completions in FCP start routine
5006 #       0  = disabled (default)
5007 #       1  = enabled
5008 # Value range is [0,1]. Default value is 0.
5009 #
5010 # This feature in under investigation and may be supported in the future.
5011 */
5012 unsigned int lpfc_fcp_look_ahead = LPFC_LOOK_AHEAD_OFF;
5013 
5014 /*
5015 # lpfc_prot_mask: i
5016 #	- Bit mask of host protection capabilities used to register with the
5017 #	  SCSI mid-layer
5018 # 	- Only meaningful if BG is turned on (lpfc_enable_bg=1).
5019 #	- Allows you to ultimately specify which profiles to use
5020 #	- Default will result in registering capabilities for all profiles.
5021 #	- SHOST_DIF_TYPE1_PROTECTION	1
5022 #		HBA supports T10 DIF Type 1: HBA to Target Type 1 Protection
5023 #	- SHOST_DIX_TYPE0_PROTECTION	8
5024 #		HBA supports DIX Type 0: Host to HBA protection only
5025 #	- SHOST_DIX_TYPE1_PROTECTION	16
5026 #		HBA supports DIX Type 1: Host to HBA  Type 1 protection
5027 #
5028 */
5029 LPFC_ATTR(prot_mask,
5030 	(SHOST_DIF_TYPE1_PROTECTION |
5031 	SHOST_DIX_TYPE0_PROTECTION |
5032 	SHOST_DIX_TYPE1_PROTECTION),
5033 	0,
5034 	(SHOST_DIF_TYPE1_PROTECTION |
5035 	SHOST_DIX_TYPE0_PROTECTION |
5036 	SHOST_DIX_TYPE1_PROTECTION),
5037 	"T10-DIF host protection capabilities mask");
5038 
5039 /*
5040 # lpfc_prot_guard: i
5041 #	- Bit mask of protection guard types to register with the SCSI mid-layer
5042 #	- Guard types are currently either 1) T10-DIF CRC 2) IP checksum
5043 #	- Allows you to ultimately specify which profiles to use
5044 #	- Default will result in registering capabilities for all guard types
5045 #
5046 */
5047 LPFC_ATTR(prot_guard,
5048 	SHOST_DIX_GUARD_IP, SHOST_DIX_GUARD_CRC, SHOST_DIX_GUARD_IP,
5049 	"T10-DIF host protection guard type");
5050 
5051 /*
5052  * Delay initial NPort discovery when Clean Address bit is cleared in
5053  * FLOGI/FDISC accept and FCID/Fabric name/Fabric portname is changed.
5054  * This parameter can have value 0 or 1.
5055  * When this parameter is set to 0, no delay is added to the initial
5056  * discovery.
5057  * When this parameter is set to non-zero value, initial Nport discovery is
5058  * delayed by ra_tov seconds when Clean Address bit is cleared in FLOGI/FDISC
5059  * accept and FCID/Fabric name/Fabric portname is changed.
5060  * Driver always delay Nport discovery for subsequent FLOGI/FDISC completion
5061  * when Clean Address bit is cleared in FLOGI/FDISC
5062  * accept and FCID/Fabric name/Fabric portname is changed.
5063  * Default value is 0.
5064  */
5065 LPFC_ATTR(delay_discovery, 0, 0, 1,
5066 	"Delay NPort discovery when Clean Address bit is cleared.");
5067 
5068 /*
5069  * lpfc_sg_seg_cnt - Initial Maximum DMA Segment Count
5070  * This value can be set to values between 64 and 4096. The default value is
5071  * 64, but may be increased to allow for larger Max I/O sizes. The scsi layer
5072  * will be allowed to request I/Os of sizes up to (MAX_SEG_COUNT * SEG_SIZE).
5073  * Because of the additional overhead involved in setting up T10-DIF,
5074  * this parameter will be limited to 128 if BlockGuard is enabled under SLI4
5075  * and will be limited to 512 if BlockGuard is enabled under SLI3.
5076  */
5077 LPFC_ATTR_R(sg_seg_cnt, LPFC_DEFAULT_SG_SEG_CNT, LPFC_DEFAULT_SG_SEG_CNT,
5078 	    LPFC_MAX_SG_SEG_CNT, "Max Scatter Gather Segment Count");
5079 
5080 /*
5081  * lpfc_enable_mds_diags: Enable MDS Diagnostics
5082  *       0  = MDS Diagnostics disabled (default)
5083  *       1  = MDS Diagnostics enabled
5084  * Value range is [0,1]. Default value is 0.
5085  */
5086 LPFC_ATTR_R(enable_mds_diags, 0, 0, 1, "Enable MDS Diagnostics");
5087 
5088 struct device_attribute *lpfc_hba_attrs[] = {
5089 	&dev_attr_nvme_info,
5090 	&dev_attr_bg_info,
5091 	&dev_attr_bg_guard_err,
5092 	&dev_attr_bg_apptag_err,
5093 	&dev_attr_bg_reftag_err,
5094 	&dev_attr_info,
5095 	&dev_attr_serialnum,
5096 	&dev_attr_modeldesc,
5097 	&dev_attr_modelname,
5098 	&dev_attr_programtype,
5099 	&dev_attr_portnum,
5100 	&dev_attr_fwrev,
5101 	&dev_attr_hdw,
5102 	&dev_attr_option_rom_version,
5103 	&dev_attr_link_state,
5104 	&dev_attr_num_discovered_ports,
5105 	&dev_attr_menlo_mgmt_mode,
5106 	&dev_attr_lpfc_drvr_version,
5107 	&dev_attr_lpfc_enable_fip,
5108 	&dev_attr_lpfc_temp_sensor,
5109 	&dev_attr_lpfc_log_verbose,
5110 	&dev_attr_lpfc_lun_queue_depth,
5111 	&dev_attr_lpfc_tgt_queue_depth,
5112 	&dev_attr_lpfc_hba_queue_depth,
5113 	&dev_attr_lpfc_peer_port_login,
5114 	&dev_attr_lpfc_nodev_tmo,
5115 	&dev_attr_lpfc_devloss_tmo,
5116 	&dev_attr_lpfc_enable_fc4_type,
5117 	&dev_attr_lpfc_xri_split,
5118 	&dev_attr_lpfc_fcp_class,
5119 	&dev_attr_lpfc_use_adisc,
5120 	&dev_attr_lpfc_first_burst_size,
5121 	&dev_attr_lpfc_ack0,
5122 	&dev_attr_lpfc_topology,
5123 	&dev_attr_lpfc_scan_down,
5124 	&dev_attr_lpfc_link_speed,
5125 	&dev_attr_lpfc_fcp_io_sched,
5126 	&dev_attr_lpfc_fcp2_no_tgt_reset,
5127 	&dev_attr_lpfc_cr_delay,
5128 	&dev_attr_lpfc_cr_count,
5129 	&dev_attr_lpfc_multi_ring_support,
5130 	&dev_attr_lpfc_multi_ring_rctl,
5131 	&dev_attr_lpfc_multi_ring_type,
5132 	&dev_attr_lpfc_fdmi_on,
5133 	&dev_attr_lpfc_enable_SmartSAN,
5134 	&dev_attr_lpfc_max_luns,
5135 	&dev_attr_lpfc_enable_npiv,
5136 	&dev_attr_lpfc_fcf_failover_policy,
5137 	&dev_attr_lpfc_enable_rrq,
5138 	&dev_attr_nport_evt_cnt,
5139 	&dev_attr_board_mode,
5140 	&dev_attr_max_vpi,
5141 	&dev_attr_used_vpi,
5142 	&dev_attr_max_rpi,
5143 	&dev_attr_used_rpi,
5144 	&dev_attr_max_xri,
5145 	&dev_attr_used_xri,
5146 	&dev_attr_npiv_info,
5147 	&dev_attr_issue_reset,
5148 	&dev_attr_lpfc_poll,
5149 	&dev_attr_lpfc_poll_tmo,
5150 	&dev_attr_lpfc_task_mgmt_tmo,
5151 	&dev_attr_lpfc_use_msi,
5152 	&dev_attr_lpfc_nvme_oas,
5153 	&dev_attr_lpfc_fcp_imax,
5154 	&dev_attr_lpfc_fcp_cpu_map,
5155 	&dev_attr_lpfc_fcp_io_channel,
5156 	&dev_attr_lpfc_suppress_rsp,
5157 	&dev_attr_lpfc_nvme_io_channel,
5158 	&dev_attr_lpfc_nvmet_mrq,
5159 	&dev_attr_lpfc_nvme_enable_fb,
5160 	&dev_attr_lpfc_nvmet_fb_size,
5161 	&dev_attr_lpfc_enable_bg,
5162 	&dev_attr_lpfc_soft_wwnn,
5163 	&dev_attr_lpfc_soft_wwpn,
5164 	&dev_attr_lpfc_soft_wwn_enable,
5165 	&dev_attr_lpfc_enable_hba_reset,
5166 	&dev_attr_lpfc_enable_hba_heartbeat,
5167 	&dev_attr_lpfc_EnableXLane,
5168 	&dev_attr_lpfc_XLanePriority,
5169 	&dev_attr_lpfc_xlane_lun,
5170 	&dev_attr_lpfc_xlane_tgt,
5171 	&dev_attr_lpfc_xlane_vpt,
5172 	&dev_attr_lpfc_xlane_lun_state,
5173 	&dev_attr_lpfc_xlane_lun_status,
5174 	&dev_attr_lpfc_xlane_priority,
5175 	&dev_attr_lpfc_sg_seg_cnt,
5176 	&dev_attr_lpfc_max_scsicmpl_time,
5177 	&dev_attr_lpfc_stat_data_ctrl,
5178 	&dev_attr_lpfc_aer_support,
5179 	&dev_attr_lpfc_aer_state_cleanup,
5180 	&dev_attr_lpfc_sriov_nr_virtfn,
5181 	&dev_attr_lpfc_req_fw_upgrade,
5182 	&dev_attr_lpfc_suppress_link_up,
5183 	&dev_attr_lpfc_iocb_cnt,
5184 	&dev_attr_iocb_hw,
5185 	&dev_attr_txq_hw,
5186 	&dev_attr_txcmplq_hw,
5187 	&dev_attr_lpfc_fips_level,
5188 	&dev_attr_lpfc_fips_rev,
5189 	&dev_attr_lpfc_dss,
5190 	&dev_attr_lpfc_sriov_hw_max_virtfn,
5191 	&dev_attr_protocol,
5192 	&dev_attr_lpfc_xlane_supported,
5193 	&dev_attr_lpfc_enable_mds_diags,
5194 	NULL,
5195 };
5196 
5197 struct device_attribute *lpfc_vport_attrs[] = {
5198 	&dev_attr_info,
5199 	&dev_attr_link_state,
5200 	&dev_attr_num_discovered_ports,
5201 	&dev_attr_lpfc_drvr_version,
5202 	&dev_attr_lpfc_log_verbose,
5203 	&dev_attr_lpfc_lun_queue_depth,
5204 	&dev_attr_lpfc_tgt_queue_depth,
5205 	&dev_attr_lpfc_nodev_tmo,
5206 	&dev_attr_lpfc_devloss_tmo,
5207 	&dev_attr_lpfc_hba_queue_depth,
5208 	&dev_attr_lpfc_peer_port_login,
5209 	&dev_attr_lpfc_restrict_login,
5210 	&dev_attr_lpfc_fcp_class,
5211 	&dev_attr_lpfc_use_adisc,
5212 	&dev_attr_lpfc_first_burst_size,
5213 	&dev_attr_lpfc_max_luns,
5214 	&dev_attr_nport_evt_cnt,
5215 	&dev_attr_npiv_info,
5216 	&dev_attr_lpfc_enable_da_id,
5217 	&dev_attr_lpfc_max_scsicmpl_time,
5218 	&dev_attr_lpfc_stat_data_ctrl,
5219 	&dev_attr_lpfc_static_vport,
5220 	&dev_attr_lpfc_fips_level,
5221 	&dev_attr_lpfc_fips_rev,
5222 	NULL,
5223 };
5224 
5225 /**
5226  * sysfs_ctlreg_write - Write method for writing to ctlreg
5227  * @filp: open sysfs file
5228  * @kobj: kernel kobject that contains the kernel class device.
5229  * @bin_attr: kernel attributes passed to us.
5230  * @buf: contains the data to be written to the adapter IOREG space.
5231  * @off: offset into buffer to beginning of data.
5232  * @count: bytes to transfer.
5233  *
5234  * Description:
5235  * Accessed via /sys/class/scsi_host/hostxxx/ctlreg.
5236  * Uses the adapter io control registers to send buf contents to the adapter.
5237  *
5238  * Returns:
5239  * -ERANGE off and count combo out of range
5240  * -EINVAL off, count or buff address invalid
5241  * -EPERM adapter is offline
5242  * value of count, buf contents written
5243  **/
5244 static ssize_t
5245 sysfs_ctlreg_write(struct file *filp, struct kobject *kobj,
5246 		   struct bin_attribute *bin_attr,
5247 		   char *buf, loff_t off, size_t count)
5248 {
5249 	size_t buf_off;
5250 	struct device *dev = container_of(kobj, struct device, kobj);
5251 	struct Scsi_Host  *shost = class_to_shost(dev);
5252 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5253 	struct lpfc_hba   *phba = vport->phba;
5254 
5255 	if (phba->sli_rev >= LPFC_SLI_REV4)
5256 		return -EPERM;
5257 
5258 	if ((off + count) > FF_REG_AREA_SIZE)
5259 		return -ERANGE;
5260 
5261 	if (count <= LPFC_REG_WRITE_KEY_SIZE)
5262 		return 0;
5263 
5264 	if (off % 4 || count % 4 || (unsigned long)buf % 4)
5265 		return -EINVAL;
5266 
5267 	/* This is to protect HBA registers from accidental writes. */
5268 	if (memcmp(buf, LPFC_REG_WRITE_KEY, LPFC_REG_WRITE_KEY_SIZE))
5269 		return -EINVAL;
5270 
5271 	if (!(vport->fc_flag & FC_OFFLINE_MODE))
5272 		return -EPERM;
5273 
5274 	spin_lock_irq(&phba->hbalock);
5275 	for (buf_off = 0; buf_off < count - LPFC_REG_WRITE_KEY_SIZE;
5276 			buf_off += sizeof(uint32_t))
5277 		writel(*((uint32_t *)(buf + buf_off + LPFC_REG_WRITE_KEY_SIZE)),
5278 		       phba->ctrl_regs_memmap_p + off + buf_off);
5279 
5280 	spin_unlock_irq(&phba->hbalock);
5281 
5282 	return count;
5283 }
5284 
5285 /**
5286  * sysfs_ctlreg_read - Read method for reading from ctlreg
5287  * @filp: open sysfs file
5288  * @kobj: kernel kobject that contains the kernel class device.
5289  * @bin_attr: kernel attributes passed to us.
5290  * @buf: if successful contains the data from the adapter IOREG space.
5291  * @off: offset into buffer to beginning of data.
5292  * @count: bytes to transfer.
5293  *
5294  * Description:
5295  * Accessed via /sys/class/scsi_host/hostxxx/ctlreg.
5296  * Uses the adapter io control registers to read data into buf.
5297  *
5298  * Returns:
5299  * -ERANGE off and count combo out of range
5300  * -EINVAL off, count or buff address invalid
5301  * value of count, buf contents read
5302  **/
5303 static ssize_t
5304 sysfs_ctlreg_read(struct file *filp, struct kobject *kobj,
5305 		  struct bin_attribute *bin_attr,
5306 		  char *buf, loff_t off, size_t count)
5307 {
5308 	size_t buf_off;
5309 	uint32_t * tmp_ptr;
5310 	struct device *dev = container_of(kobj, struct device, kobj);
5311 	struct Scsi_Host  *shost = class_to_shost(dev);
5312 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5313 	struct lpfc_hba   *phba = vport->phba;
5314 
5315 	if (phba->sli_rev >= LPFC_SLI_REV4)
5316 		return -EPERM;
5317 
5318 	if (off > FF_REG_AREA_SIZE)
5319 		return -ERANGE;
5320 
5321 	if ((off + count) > FF_REG_AREA_SIZE)
5322 		count = FF_REG_AREA_SIZE - off;
5323 
5324 	if (count == 0) return 0;
5325 
5326 	if (off % 4 || count % 4 || (unsigned long)buf % 4)
5327 		return -EINVAL;
5328 
5329 	spin_lock_irq(&phba->hbalock);
5330 
5331 	for (buf_off = 0; buf_off < count; buf_off += sizeof(uint32_t)) {
5332 		tmp_ptr = (uint32_t *)(buf + buf_off);
5333 		*tmp_ptr = readl(phba->ctrl_regs_memmap_p + off + buf_off);
5334 	}
5335 
5336 	spin_unlock_irq(&phba->hbalock);
5337 
5338 	return count;
5339 }
5340 
5341 static struct bin_attribute sysfs_ctlreg_attr = {
5342 	.attr = {
5343 		.name = "ctlreg",
5344 		.mode = S_IRUSR | S_IWUSR,
5345 	},
5346 	.size = 256,
5347 	.read = sysfs_ctlreg_read,
5348 	.write = sysfs_ctlreg_write,
5349 };
5350 
5351 /**
5352  * sysfs_mbox_write - Write method for writing information via mbox
5353  * @filp: open sysfs file
5354  * @kobj: kernel kobject that contains the kernel class device.
5355  * @bin_attr: kernel attributes passed to us.
5356  * @buf: contains the data to be written to sysfs mbox.
5357  * @off: offset into buffer to beginning of data.
5358  * @count: bytes to transfer.
5359  *
5360  * Description:
5361  * Deprecated function. All mailbox access from user space is performed via the
5362  * bsg interface.
5363  *
5364  * Returns:
5365  * -EPERM operation not permitted
5366  **/
5367 static ssize_t
5368 sysfs_mbox_write(struct file *filp, struct kobject *kobj,
5369 		 struct bin_attribute *bin_attr,
5370 		 char *buf, loff_t off, size_t count)
5371 {
5372 	return -EPERM;
5373 }
5374 
5375 /**
5376  * sysfs_mbox_read - Read method for reading information via mbox
5377  * @filp: open sysfs file
5378  * @kobj: kernel kobject that contains the kernel class device.
5379  * @bin_attr: kernel attributes passed to us.
5380  * @buf: contains the data to be read from sysfs mbox.
5381  * @off: offset into buffer to beginning of data.
5382  * @count: bytes to transfer.
5383  *
5384  * Description:
5385  * Deprecated function. All mailbox access from user space is performed via the
5386  * bsg interface.
5387  *
5388  * Returns:
5389  * -EPERM operation not permitted
5390  **/
5391 static ssize_t
5392 sysfs_mbox_read(struct file *filp, struct kobject *kobj,
5393 		struct bin_attribute *bin_attr,
5394 		char *buf, loff_t off, size_t count)
5395 {
5396 	return -EPERM;
5397 }
5398 
5399 static struct bin_attribute sysfs_mbox_attr = {
5400 	.attr = {
5401 		.name = "mbox",
5402 		.mode = S_IRUSR | S_IWUSR,
5403 	},
5404 	.size = MAILBOX_SYSFS_MAX,
5405 	.read = sysfs_mbox_read,
5406 	.write = sysfs_mbox_write,
5407 };
5408 
5409 /**
5410  * lpfc_alloc_sysfs_attr - Creates the ctlreg and mbox entries
5411  * @vport: address of lpfc vport structure.
5412  *
5413  * Return codes:
5414  * zero on success
5415  * error return code from sysfs_create_bin_file()
5416  **/
5417 int
5418 lpfc_alloc_sysfs_attr(struct lpfc_vport *vport)
5419 {
5420 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5421 	int error;
5422 
5423 	error = sysfs_create_bin_file(&shost->shost_dev.kobj,
5424 				      &sysfs_drvr_stat_data_attr);
5425 
5426 	/* Virtual ports do not need ctrl_reg and mbox */
5427 	if (error || vport->port_type == LPFC_NPIV_PORT)
5428 		goto out;
5429 
5430 	error = sysfs_create_bin_file(&shost->shost_dev.kobj,
5431 				      &sysfs_ctlreg_attr);
5432 	if (error)
5433 		goto out_remove_stat_attr;
5434 
5435 	error = sysfs_create_bin_file(&shost->shost_dev.kobj,
5436 				      &sysfs_mbox_attr);
5437 	if (error)
5438 		goto out_remove_ctlreg_attr;
5439 
5440 	return 0;
5441 out_remove_ctlreg_attr:
5442 	sysfs_remove_bin_file(&shost->shost_dev.kobj, &sysfs_ctlreg_attr);
5443 out_remove_stat_attr:
5444 	sysfs_remove_bin_file(&shost->shost_dev.kobj,
5445 			&sysfs_drvr_stat_data_attr);
5446 out:
5447 	return error;
5448 }
5449 
5450 /**
5451  * lpfc_free_sysfs_attr - Removes the ctlreg and mbox entries
5452  * @vport: address of lpfc vport structure.
5453  **/
5454 void
5455 lpfc_free_sysfs_attr(struct lpfc_vport *vport)
5456 {
5457 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5458 	sysfs_remove_bin_file(&shost->shost_dev.kobj,
5459 		&sysfs_drvr_stat_data_attr);
5460 	/* Virtual ports do not need ctrl_reg and mbox */
5461 	if (vport->port_type == LPFC_NPIV_PORT)
5462 		return;
5463 	sysfs_remove_bin_file(&shost->shost_dev.kobj, &sysfs_mbox_attr);
5464 	sysfs_remove_bin_file(&shost->shost_dev.kobj, &sysfs_ctlreg_attr);
5465 }
5466 
5467 /*
5468  * Dynamic FC Host Attributes Support
5469  */
5470 
5471 /**
5472  * lpfc_get_host_symbolic_name - Copy symbolic name into the scsi host
5473  * @shost: kernel scsi host pointer.
5474  **/
5475 static void
5476 lpfc_get_host_symbolic_name(struct Scsi_Host *shost)
5477 {
5478 	struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
5479 
5480 	lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
5481 				      sizeof fc_host_symbolic_name(shost));
5482 }
5483 
5484 /**
5485  * lpfc_get_host_port_id - Copy the vport DID into the scsi host port id
5486  * @shost: kernel scsi host pointer.
5487  **/
5488 static void
5489 lpfc_get_host_port_id(struct Scsi_Host *shost)
5490 {
5491 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5492 
5493 	/* note: fc_myDID already in cpu endianness */
5494 	fc_host_port_id(shost) = vport->fc_myDID;
5495 }
5496 
5497 /**
5498  * lpfc_get_host_port_type - Set the value of the scsi host port type
5499  * @shost: kernel scsi host pointer.
5500  **/
5501 static void
5502 lpfc_get_host_port_type(struct Scsi_Host *shost)
5503 {
5504 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5505 	struct lpfc_hba   *phba = vport->phba;
5506 
5507 	spin_lock_irq(shost->host_lock);
5508 
5509 	if (vport->port_type == LPFC_NPIV_PORT) {
5510 		fc_host_port_type(shost) = FC_PORTTYPE_NPIV;
5511 	} else if (lpfc_is_link_up(phba)) {
5512 		if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
5513 			if (vport->fc_flag & FC_PUBLIC_LOOP)
5514 				fc_host_port_type(shost) = FC_PORTTYPE_NLPORT;
5515 			else
5516 				fc_host_port_type(shost) = FC_PORTTYPE_LPORT;
5517 		} else {
5518 			if (vport->fc_flag & FC_FABRIC)
5519 				fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
5520 			else
5521 				fc_host_port_type(shost) = FC_PORTTYPE_PTP;
5522 		}
5523 	} else
5524 		fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
5525 
5526 	spin_unlock_irq(shost->host_lock);
5527 }
5528 
5529 /**
5530  * lpfc_get_host_port_state - Set the value of the scsi host port state
5531  * @shost: kernel scsi host pointer.
5532  **/
5533 static void
5534 lpfc_get_host_port_state(struct Scsi_Host *shost)
5535 {
5536 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5537 	struct lpfc_hba   *phba = vport->phba;
5538 
5539 	spin_lock_irq(shost->host_lock);
5540 
5541 	if (vport->fc_flag & FC_OFFLINE_MODE)
5542 		fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
5543 	else {
5544 		switch (phba->link_state) {
5545 		case LPFC_LINK_UNKNOWN:
5546 		case LPFC_LINK_DOWN:
5547 			fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
5548 			break;
5549 		case LPFC_LINK_UP:
5550 		case LPFC_CLEAR_LA:
5551 		case LPFC_HBA_READY:
5552 			/* Links up, reports port state accordingly */
5553 			if (vport->port_state < LPFC_VPORT_READY)
5554 				fc_host_port_state(shost) =
5555 							FC_PORTSTATE_BYPASSED;
5556 			else
5557 				fc_host_port_state(shost) =
5558 							FC_PORTSTATE_ONLINE;
5559 			break;
5560 		case LPFC_HBA_ERROR:
5561 			fc_host_port_state(shost) = FC_PORTSTATE_ERROR;
5562 			break;
5563 		default:
5564 			fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
5565 			break;
5566 		}
5567 	}
5568 
5569 	spin_unlock_irq(shost->host_lock);
5570 }
5571 
5572 /**
5573  * lpfc_get_host_speed - Set the value of the scsi host speed
5574  * @shost: kernel scsi host pointer.
5575  **/
5576 static void
5577 lpfc_get_host_speed(struct Scsi_Host *shost)
5578 {
5579 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5580 	struct lpfc_hba   *phba = vport->phba;
5581 
5582 	spin_lock_irq(shost->host_lock);
5583 
5584 	if ((lpfc_is_link_up(phba)) && (!(phba->hba_flag & HBA_FCOE_MODE))) {
5585 		switch(phba->fc_linkspeed) {
5586 		case LPFC_LINK_SPEED_1GHZ:
5587 			fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
5588 			break;
5589 		case LPFC_LINK_SPEED_2GHZ:
5590 			fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
5591 			break;
5592 		case LPFC_LINK_SPEED_4GHZ:
5593 			fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
5594 			break;
5595 		case LPFC_LINK_SPEED_8GHZ:
5596 			fc_host_speed(shost) = FC_PORTSPEED_8GBIT;
5597 			break;
5598 		case LPFC_LINK_SPEED_10GHZ:
5599 			fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
5600 			break;
5601 		case LPFC_LINK_SPEED_16GHZ:
5602 			fc_host_speed(shost) = FC_PORTSPEED_16GBIT;
5603 			break;
5604 		case LPFC_LINK_SPEED_32GHZ:
5605 			fc_host_speed(shost) = FC_PORTSPEED_32GBIT;
5606 			break;
5607 		default:
5608 			fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
5609 			break;
5610 		}
5611 	} else
5612 		fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
5613 
5614 	spin_unlock_irq(shost->host_lock);
5615 }
5616 
5617 /**
5618  * lpfc_get_host_fabric_name - Set the value of the scsi host fabric name
5619  * @shost: kernel scsi host pointer.
5620  **/
5621 static void
5622 lpfc_get_host_fabric_name (struct Scsi_Host *shost)
5623 {
5624 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5625 	struct lpfc_hba   *phba = vport->phba;
5626 	u64 node_name;
5627 
5628 	spin_lock_irq(shost->host_lock);
5629 
5630 	if ((vport->port_state > LPFC_FLOGI) &&
5631 	    ((vport->fc_flag & FC_FABRIC) ||
5632 	     ((phba->fc_topology == LPFC_TOPOLOGY_LOOP) &&
5633 	      (vport->fc_flag & FC_PUBLIC_LOOP))))
5634 		node_name = wwn_to_u64(phba->fc_fabparam.nodeName.u.wwn);
5635 	else
5636 		/* fabric is local port if there is no F/FL_Port */
5637 		node_name = 0;
5638 
5639 	spin_unlock_irq(shost->host_lock);
5640 
5641 	fc_host_fabric_name(shost) = node_name;
5642 }
5643 
5644 /**
5645  * lpfc_get_stats - Return statistical information about the adapter
5646  * @shost: kernel scsi host pointer.
5647  *
5648  * Notes:
5649  * NULL on error for link down, no mbox pool, sli2 active,
5650  * management not allowed, memory allocation error, or mbox error.
5651  *
5652  * Returns:
5653  * NULL for error
5654  * address of the adapter host statistics
5655  **/
5656 static struct fc_host_statistics *
5657 lpfc_get_stats(struct Scsi_Host *shost)
5658 {
5659 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5660 	struct lpfc_hba   *phba = vport->phba;
5661 	struct lpfc_sli   *psli = &phba->sli;
5662 	struct fc_host_statistics *hs = &phba->link_stats;
5663 	struct lpfc_lnk_stat * lso = &psli->lnk_stat_offsets;
5664 	LPFC_MBOXQ_t *pmboxq;
5665 	MAILBOX_t *pmb;
5666 	unsigned long seconds;
5667 	int rc = 0;
5668 
5669 	/*
5670 	 * prevent udev from issuing mailbox commands until the port is
5671 	 * configured.
5672 	 */
5673 	if (phba->link_state < LPFC_LINK_DOWN ||
5674 	    !phba->mbox_mem_pool ||
5675 	    (phba->sli.sli_flag & LPFC_SLI_ACTIVE) == 0)
5676 		return NULL;
5677 
5678 	if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO)
5679 		return NULL;
5680 
5681 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5682 	if (!pmboxq)
5683 		return NULL;
5684 	memset(pmboxq, 0, sizeof (LPFC_MBOXQ_t));
5685 
5686 	pmb = &pmboxq->u.mb;
5687 	pmb->mbxCommand = MBX_READ_STATUS;
5688 	pmb->mbxOwner = OWN_HOST;
5689 	pmboxq->context1 = NULL;
5690 	pmboxq->vport = vport;
5691 
5692 	if (vport->fc_flag & FC_OFFLINE_MODE)
5693 		rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
5694 	else
5695 		rc = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
5696 
5697 	if (rc != MBX_SUCCESS) {
5698 		if (rc != MBX_TIMEOUT)
5699 			mempool_free(pmboxq, phba->mbox_mem_pool);
5700 		return NULL;
5701 	}
5702 
5703 	memset(hs, 0, sizeof (struct fc_host_statistics));
5704 
5705 	hs->tx_frames = pmb->un.varRdStatus.xmitFrameCnt;
5706 	/*
5707 	 * The MBX_READ_STATUS returns tx_k_bytes which has to
5708 	 * converted to words
5709 	 */
5710 	hs->tx_words = (uint64_t)
5711 			((uint64_t)pmb->un.varRdStatus.xmitByteCnt
5712 			* (uint64_t)256);
5713 	hs->rx_frames = pmb->un.varRdStatus.rcvFrameCnt;
5714 	hs->rx_words = (uint64_t)
5715 			((uint64_t)pmb->un.varRdStatus.rcvByteCnt
5716 			 * (uint64_t)256);
5717 
5718 	memset(pmboxq, 0, sizeof (LPFC_MBOXQ_t));
5719 	pmb->mbxCommand = MBX_READ_LNK_STAT;
5720 	pmb->mbxOwner = OWN_HOST;
5721 	pmboxq->context1 = NULL;
5722 	pmboxq->vport = vport;
5723 
5724 	if (vport->fc_flag & FC_OFFLINE_MODE)
5725 		rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
5726 	else
5727 		rc = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
5728 
5729 	if (rc != MBX_SUCCESS) {
5730 		if (rc != MBX_TIMEOUT)
5731 			mempool_free(pmboxq, phba->mbox_mem_pool);
5732 		return NULL;
5733 	}
5734 
5735 	hs->link_failure_count = pmb->un.varRdLnk.linkFailureCnt;
5736 	hs->loss_of_sync_count = pmb->un.varRdLnk.lossSyncCnt;
5737 	hs->loss_of_signal_count = pmb->un.varRdLnk.lossSignalCnt;
5738 	hs->prim_seq_protocol_err_count = pmb->un.varRdLnk.primSeqErrCnt;
5739 	hs->invalid_tx_word_count = pmb->un.varRdLnk.invalidXmitWord;
5740 	hs->invalid_crc_count = pmb->un.varRdLnk.crcCnt;
5741 	hs->error_frames = pmb->un.varRdLnk.crcCnt;
5742 
5743 	hs->link_failure_count -= lso->link_failure_count;
5744 	hs->loss_of_sync_count -= lso->loss_of_sync_count;
5745 	hs->loss_of_signal_count -= lso->loss_of_signal_count;
5746 	hs->prim_seq_protocol_err_count -= lso->prim_seq_protocol_err_count;
5747 	hs->invalid_tx_word_count -= lso->invalid_tx_word_count;
5748 	hs->invalid_crc_count -= lso->invalid_crc_count;
5749 	hs->error_frames -= lso->error_frames;
5750 
5751 	if (phba->hba_flag & HBA_FCOE_MODE) {
5752 		hs->lip_count = -1;
5753 		hs->nos_count = (phba->link_events >> 1);
5754 		hs->nos_count -= lso->link_events;
5755 	} else if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
5756 		hs->lip_count = (phba->fc_eventTag >> 1);
5757 		hs->lip_count -= lso->link_events;
5758 		hs->nos_count = -1;
5759 	} else {
5760 		hs->lip_count = -1;
5761 		hs->nos_count = (phba->fc_eventTag >> 1);
5762 		hs->nos_count -= lso->link_events;
5763 	}
5764 
5765 	hs->dumped_frames = -1;
5766 
5767 	seconds = get_seconds();
5768 	if (seconds < psli->stats_start)
5769 		hs->seconds_since_last_reset = seconds +
5770 				((unsigned long)-1 - psli->stats_start);
5771 	else
5772 		hs->seconds_since_last_reset = seconds - psli->stats_start;
5773 
5774 	mempool_free(pmboxq, phba->mbox_mem_pool);
5775 
5776 	return hs;
5777 }
5778 
5779 /**
5780  * lpfc_reset_stats - Copy the adapter link stats information
5781  * @shost: kernel scsi host pointer.
5782  **/
5783 static void
5784 lpfc_reset_stats(struct Scsi_Host *shost)
5785 {
5786 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5787 	struct lpfc_hba   *phba = vport->phba;
5788 	struct lpfc_sli   *psli = &phba->sli;
5789 	struct lpfc_lnk_stat *lso = &psli->lnk_stat_offsets;
5790 	LPFC_MBOXQ_t *pmboxq;
5791 	MAILBOX_t *pmb;
5792 	int rc = 0;
5793 
5794 	if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO)
5795 		return;
5796 
5797 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5798 	if (!pmboxq)
5799 		return;
5800 	memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
5801 
5802 	pmb = &pmboxq->u.mb;
5803 	pmb->mbxCommand = MBX_READ_STATUS;
5804 	pmb->mbxOwner = OWN_HOST;
5805 	pmb->un.varWords[0] = 0x1; /* reset request */
5806 	pmboxq->context1 = NULL;
5807 	pmboxq->vport = vport;
5808 
5809 	if ((vport->fc_flag & FC_OFFLINE_MODE) ||
5810 		(!(psli->sli_flag & LPFC_SLI_ACTIVE)))
5811 		rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
5812 	else
5813 		rc = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
5814 
5815 	if (rc != MBX_SUCCESS) {
5816 		if (rc != MBX_TIMEOUT)
5817 			mempool_free(pmboxq, phba->mbox_mem_pool);
5818 		return;
5819 	}
5820 
5821 	memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
5822 	pmb->mbxCommand = MBX_READ_LNK_STAT;
5823 	pmb->mbxOwner = OWN_HOST;
5824 	pmboxq->context1 = NULL;
5825 	pmboxq->vport = vport;
5826 
5827 	if ((vport->fc_flag & FC_OFFLINE_MODE) ||
5828 	    (!(psli->sli_flag & LPFC_SLI_ACTIVE)))
5829 		rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
5830 	else
5831 		rc = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
5832 
5833 	if (rc != MBX_SUCCESS) {
5834 		if (rc != MBX_TIMEOUT)
5835 			mempool_free( pmboxq, phba->mbox_mem_pool);
5836 		return;
5837 	}
5838 
5839 	lso->link_failure_count = pmb->un.varRdLnk.linkFailureCnt;
5840 	lso->loss_of_sync_count = pmb->un.varRdLnk.lossSyncCnt;
5841 	lso->loss_of_signal_count = pmb->un.varRdLnk.lossSignalCnt;
5842 	lso->prim_seq_protocol_err_count = pmb->un.varRdLnk.primSeqErrCnt;
5843 	lso->invalid_tx_word_count = pmb->un.varRdLnk.invalidXmitWord;
5844 	lso->invalid_crc_count = pmb->un.varRdLnk.crcCnt;
5845 	lso->error_frames = pmb->un.varRdLnk.crcCnt;
5846 	if (phba->hba_flag & HBA_FCOE_MODE)
5847 		lso->link_events = (phba->link_events >> 1);
5848 	else
5849 		lso->link_events = (phba->fc_eventTag >> 1);
5850 
5851 	psli->stats_start = get_seconds();
5852 
5853 	mempool_free(pmboxq, phba->mbox_mem_pool);
5854 
5855 	return;
5856 }
5857 
5858 /*
5859  * The LPFC driver treats linkdown handling as target loss events so there
5860  * are no sysfs handlers for link_down_tmo.
5861  */
5862 
5863 /**
5864  * lpfc_get_node_by_target - Return the nodelist for a target
5865  * @starget: kernel scsi target pointer.
5866  *
5867  * Returns:
5868  * address of the node list if found
5869  * NULL target not found
5870  **/
5871 static struct lpfc_nodelist *
5872 lpfc_get_node_by_target(struct scsi_target *starget)
5873 {
5874 	struct Scsi_Host  *shost = dev_to_shost(starget->dev.parent);
5875 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5876 	struct lpfc_nodelist *ndlp;
5877 
5878 	spin_lock_irq(shost->host_lock);
5879 	/* Search for this, mapped, target ID */
5880 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5881 		if (NLP_CHK_NODE_ACT(ndlp) &&
5882 		    ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
5883 		    starget->id == ndlp->nlp_sid) {
5884 			spin_unlock_irq(shost->host_lock);
5885 			return ndlp;
5886 		}
5887 	}
5888 	spin_unlock_irq(shost->host_lock);
5889 	return NULL;
5890 }
5891 
5892 /**
5893  * lpfc_get_starget_port_id - Set the target port id to the ndlp DID or -1
5894  * @starget: kernel scsi target pointer.
5895  **/
5896 static void
5897 lpfc_get_starget_port_id(struct scsi_target *starget)
5898 {
5899 	struct lpfc_nodelist *ndlp = lpfc_get_node_by_target(starget);
5900 
5901 	fc_starget_port_id(starget) = ndlp ? ndlp->nlp_DID : -1;
5902 }
5903 
5904 /**
5905  * lpfc_get_starget_node_name - Set the target node name
5906  * @starget: kernel scsi target pointer.
5907  *
5908  * Description: Set the target node name to the ndlp node name wwn or zero.
5909  **/
5910 static void
5911 lpfc_get_starget_node_name(struct scsi_target *starget)
5912 {
5913 	struct lpfc_nodelist *ndlp = lpfc_get_node_by_target(starget);
5914 
5915 	fc_starget_node_name(starget) =
5916 		ndlp ? wwn_to_u64(ndlp->nlp_nodename.u.wwn) : 0;
5917 }
5918 
5919 /**
5920  * lpfc_get_starget_port_name - Set the target port name
5921  * @starget: kernel scsi target pointer.
5922  *
5923  * Description:  set the target port name to the ndlp port name wwn or zero.
5924  **/
5925 static void
5926 lpfc_get_starget_port_name(struct scsi_target *starget)
5927 {
5928 	struct lpfc_nodelist *ndlp = lpfc_get_node_by_target(starget);
5929 
5930 	fc_starget_port_name(starget) =
5931 		ndlp ? wwn_to_u64(ndlp->nlp_portname.u.wwn) : 0;
5932 }
5933 
5934 /**
5935  * lpfc_set_rport_loss_tmo - Set the rport dev loss tmo
5936  * @rport: fc rport address.
5937  * @timeout: new value for dev loss tmo.
5938  *
5939  * Description:
5940  * If timeout is non zero set the dev_loss_tmo to timeout, else set
5941  * dev_loss_tmo to one.
5942  **/
5943 static void
5944 lpfc_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
5945 {
5946 	if (timeout)
5947 		rport->dev_loss_tmo = timeout;
5948 	else
5949 		rport->dev_loss_tmo = 1;
5950 }
5951 
5952 /**
5953  * lpfc_rport_show_function - Return rport target information
5954  *
5955  * Description:
5956  * Macro that uses field to generate a function with the name lpfc_show_rport_
5957  *
5958  * lpfc_show_rport_##field: returns the bytes formatted in buf
5959  * @cdev: class converted to an fc_rport.
5960  * @buf: on return contains the target_field or zero.
5961  *
5962  * Returns: size of formatted string.
5963  **/
5964 #define lpfc_rport_show_function(field, format_string, sz, cast)	\
5965 static ssize_t								\
5966 lpfc_show_rport_##field (struct device *dev,				\
5967 			 struct device_attribute *attr,			\
5968 			 char *buf)					\
5969 {									\
5970 	struct fc_rport *rport = transport_class_to_rport(dev);		\
5971 	struct lpfc_rport_data *rdata = rport->hostdata;		\
5972 	return snprintf(buf, sz, format_string,				\
5973 		(rdata->target) ? cast rdata->target->field : 0);	\
5974 }
5975 
5976 #define lpfc_rport_rd_attr(field, format_string, sz)			\
5977 	lpfc_rport_show_function(field, format_string, sz, )		\
5978 static FC_RPORT_ATTR(field, S_IRUGO, lpfc_show_rport_##field, NULL)
5979 
5980 /**
5981  * lpfc_set_vport_symbolic_name - Set the vport's symbolic name
5982  * @fc_vport: The fc_vport who's symbolic name has been changed.
5983  *
5984  * Description:
5985  * This function is called by the transport after the @fc_vport's symbolic name
5986  * has been changed. This function re-registers the symbolic name with the
5987  * switch to propagate the change into the fabric if the vport is active.
5988  **/
5989 static void
5990 lpfc_set_vport_symbolic_name(struct fc_vport *fc_vport)
5991 {
5992 	struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
5993 
5994 	if (vport->port_state == LPFC_VPORT_READY)
5995 		lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
5996 }
5997 
5998 /**
5999  * lpfc_hba_log_verbose_init - Set hba's log verbose level
6000  * @phba: Pointer to lpfc_hba struct.
6001  *
6002  * This function is called by the lpfc_get_cfgparam() routine to set the
6003  * module lpfc_log_verbose into the @phba cfg_log_verbose for use with
6004  * log message according to the module's lpfc_log_verbose parameter setting
6005  * before hba port or vport created.
6006  **/
6007 static void
6008 lpfc_hba_log_verbose_init(struct lpfc_hba *phba, uint32_t verbose)
6009 {
6010 	phba->cfg_log_verbose = verbose;
6011 }
6012 
6013 struct fc_function_template lpfc_transport_functions = {
6014 	/* fixed attributes the driver supports */
6015 	.show_host_node_name = 1,
6016 	.show_host_port_name = 1,
6017 	.show_host_supported_classes = 1,
6018 	.show_host_supported_fc4s = 1,
6019 	.show_host_supported_speeds = 1,
6020 	.show_host_maxframe_size = 1,
6021 
6022 	.get_host_symbolic_name = lpfc_get_host_symbolic_name,
6023 	.show_host_symbolic_name = 1,
6024 
6025 	/* dynamic attributes the driver supports */
6026 	.get_host_port_id = lpfc_get_host_port_id,
6027 	.show_host_port_id = 1,
6028 
6029 	.get_host_port_type = lpfc_get_host_port_type,
6030 	.show_host_port_type = 1,
6031 
6032 	.get_host_port_state = lpfc_get_host_port_state,
6033 	.show_host_port_state = 1,
6034 
6035 	/* active_fc4s is shown but doesn't change (thus no get function) */
6036 	.show_host_active_fc4s = 1,
6037 
6038 	.get_host_speed = lpfc_get_host_speed,
6039 	.show_host_speed = 1,
6040 
6041 	.get_host_fabric_name = lpfc_get_host_fabric_name,
6042 	.show_host_fabric_name = 1,
6043 
6044 	/*
6045 	 * The LPFC driver treats linkdown handling as target loss events
6046 	 * so there are no sysfs handlers for link_down_tmo.
6047 	 */
6048 
6049 	.get_fc_host_stats = lpfc_get_stats,
6050 	.reset_fc_host_stats = lpfc_reset_stats,
6051 
6052 	.dd_fcrport_size = sizeof(struct lpfc_rport_data),
6053 	.show_rport_maxframe_size = 1,
6054 	.show_rport_supported_classes = 1,
6055 
6056 	.set_rport_dev_loss_tmo = lpfc_set_rport_loss_tmo,
6057 	.show_rport_dev_loss_tmo = 1,
6058 
6059 	.get_starget_port_id  = lpfc_get_starget_port_id,
6060 	.show_starget_port_id = 1,
6061 
6062 	.get_starget_node_name = lpfc_get_starget_node_name,
6063 	.show_starget_node_name = 1,
6064 
6065 	.get_starget_port_name = lpfc_get_starget_port_name,
6066 	.show_starget_port_name = 1,
6067 
6068 	.issue_fc_host_lip = lpfc_issue_lip,
6069 	.dev_loss_tmo_callbk = lpfc_dev_loss_tmo_callbk,
6070 	.terminate_rport_io = lpfc_terminate_rport_io,
6071 
6072 	.dd_fcvport_size = sizeof(struct lpfc_vport *),
6073 
6074 	.vport_disable = lpfc_vport_disable,
6075 
6076 	.set_vport_symbolic_name = lpfc_set_vport_symbolic_name,
6077 
6078 	.bsg_request = lpfc_bsg_request,
6079 	.bsg_timeout = lpfc_bsg_timeout,
6080 };
6081 
6082 struct fc_function_template lpfc_vport_transport_functions = {
6083 	/* fixed attributes the driver supports */
6084 	.show_host_node_name = 1,
6085 	.show_host_port_name = 1,
6086 	.show_host_supported_classes = 1,
6087 	.show_host_supported_fc4s = 1,
6088 	.show_host_supported_speeds = 1,
6089 	.show_host_maxframe_size = 1,
6090 
6091 	.get_host_symbolic_name = lpfc_get_host_symbolic_name,
6092 	.show_host_symbolic_name = 1,
6093 
6094 	/* dynamic attributes the driver supports */
6095 	.get_host_port_id = lpfc_get_host_port_id,
6096 	.show_host_port_id = 1,
6097 
6098 	.get_host_port_type = lpfc_get_host_port_type,
6099 	.show_host_port_type = 1,
6100 
6101 	.get_host_port_state = lpfc_get_host_port_state,
6102 	.show_host_port_state = 1,
6103 
6104 	/* active_fc4s is shown but doesn't change (thus no get function) */
6105 	.show_host_active_fc4s = 1,
6106 
6107 	.get_host_speed = lpfc_get_host_speed,
6108 	.show_host_speed = 1,
6109 
6110 	.get_host_fabric_name = lpfc_get_host_fabric_name,
6111 	.show_host_fabric_name = 1,
6112 
6113 	/*
6114 	 * The LPFC driver treats linkdown handling as target loss events
6115 	 * so there are no sysfs handlers for link_down_tmo.
6116 	 */
6117 
6118 	.get_fc_host_stats = lpfc_get_stats,
6119 	.reset_fc_host_stats = lpfc_reset_stats,
6120 
6121 	.dd_fcrport_size = sizeof(struct lpfc_rport_data),
6122 	.show_rport_maxframe_size = 1,
6123 	.show_rport_supported_classes = 1,
6124 
6125 	.set_rport_dev_loss_tmo = lpfc_set_rport_loss_tmo,
6126 	.show_rport_dev_loss_tmo = 1,
6127 
6128 	.get_starget_port_id  = lpfc_get_starget_port_id,
6129 	.show_starget_port_id = 1,
6130 
6131 	.get_starget_node_name = lpfc_get_starget_node_name,
6132 	.show_starget_node_name = 1,
6133 
6134 	.get_starget_port_name = lpfc_get_starget_port_name,
6135 	.show_starget_port_name = 1,
6136 
6137 	.dev_loss_tmo_callbk = lpfc_dev_loss_tmo_callbk,
6138 	.terminate_rport_io = lpfc_terminate_rport_io,
6139 
6140 	.vport_disable = lpfc_vport_disable,
6141 
6142 	.set_vport_symbolic_name = lpfc_set_vport_symbolic_name,
6143 };
6144 
6145 /**
6146  * lpfc_get_cfgparam - Used during probe_one to init the adapter structure
6147  * @phba: lpfc_hba pointer.
6148  **/
6149 void
6150 lpfc_get_cfgparam(struct lpfc_hba *phba)
6151 {
6152 	lpfc_fcp_io_sched_init(phba, lpfc_fcp_io_sched);
6153 	lpfc_fcp2_no_tgt_reset_init(phba, lpfc_fcp2_no_tgt_reset);
6154 	lpfc_cr_delay_init(phba, lpfc_cr_delay);
6155 	lpfc_cr_count_init(phba, lpfc_cr_count);
6156 	lpfc_multi_ring_support_init(phba, lpfc_multi_ring_support);
6157 	lpfc_multi_ring_rctl_init(phba, lpfc_multi_ring_rctl);
6158 	lpfc_multi_ring_type_init(phba, lpfc_multi_ring_type);
6159 	lpfc_ack0_init(phba, lpfc_ack0);
6160 	lpfc_topology_init(phba, lpfc_topology);
6161 	lpfc_link_speed_init(phba, lpfc_link_speed);
6162 	lpfc_poll_tmo_init(phba, lpfc_poll_tmo);
6163 	lpfc_task_mgmt_tmo_init(phba, lpfc_task_mgmt_tmo);
6164 	lpfc_enable_npiv_init(phba, lpfc_enable_npiv);
6165 	lpfc_fcf_failover_policy_init(phba, lpfc_fcf_failover_policy);
6166 	lpfc_enable_rrq_init(phba, lpfc_enable_rrq);
6167 	lpfc_fdmi_on_init(phba, lpfc_fdmi_on);
6168 	lpfc_enable_SmartSAN_init(phba, lpfc_enable_SmartSAN);
6169 	lpfc_use_msi_init(phba, lpfc_use_msi);
6170 	lpfc_nvme_oas_init(phba, lpfc_nvme_oas);
6171 	lpfc_fcp_imax_init(phba, lpfc_fcp_imax);
6172 	lpfc_fcp_cpu_map_init(phba, lpfc_fcp_cpu_map);
6173 	lpfc_enable_hba_reset_init(phba, lpfc_enable_hba_reset);
6174 	lpfc_enable_hba_heartbeat_init(phba, lpfc_enable_hba_heartbeat);
6175 
6176 	lpfc_EnableXLane_init(phba, lpfc_EnableXLane);
6177 	if (phba->sli_rev != LPFC_SLI_REV4)
6178 		phba->cfg_EnableXLane = 0;
6179 	lpfc_XLanePriority_init(phba, lpfc_XLanePriority);
6180 
6181 	memset(phba->cfg_oas_tgt_wwpn, 0, (8 * sizeof(uint8_t)));
6182 	memset(phba->cfg_oas_vpt_wwpn, 0, (8 * sizeof(uint8_t)));
6183 	phba->cfg_oas_lun_state = 0;
6184 	phba->cfg_oas_lun_status = 0;
6185 	phba->cfg_oas_flags = 0;
6186 	phba->cfg_oas_priority = 0;
6187 	lpfc_enable_bg_init(phba, lpfc_enable_bg);
6188 	lpfc_prot_mask_init(phba, lpfc_prot_mask);
6189 	lpfc_prot_guard_init(phba, lpfc_prot_guard);
6190 	if (phba->sli_rev == LPFC_SLI_REV4)
6191 		phba->cfg_poll = 0;
6192 	else
6193 		phba->cfg_poll = lpfc_poll;
6194 	lpfc_suppress_rsp_init(phba, lpfc_suppress_rsp);
6195 
6196 	lpfc_enable_fc4_type_init(phba, lpfc_enable_fc4_type);
6197 	lpfc_nvmet_mrq_init(phba, lpfc_nvmet_mrq);
6198 
6199 	/* Initialize first burst. Target vs Initiator are different. */
6200 	lpfc_nvme_enable_fb_init(phba, lpfc_nvme_enable_fb);
6201 	lpfc_nvmet_fb_size_init(phba, lpfc_nvmet_fb_size);
6202 	lpfc_fcp_io_channel_init(phba, lpfc_fcp_io_channel);
6203 	lpfc_nvme_io_channel_init(phba, lpfc_nvme_io_channel);
6204 
6205 	if (phba->sli_rev != LPFC_SLI_REV4) {
6206 		/* NVME only supported on SLI4 */
6207 		phba->nvmet_support = 0;
6208 		phba->cfg_enable_fc4_type = LPFC_ENABLE_FCP;
6209 	} else {
6210 		/* We MUST have FCP support */
6211 		if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
6212 			phba->cfg_enable_fc4_type |= LPFC_ENABLE_FCP;
6213 	}
6214 
6215 	/* A value of 0 means use the number of CPUs found in the system */
6216 	if (phba->cfg_fcp_io_channel == 0)
6217 		phba->cfg_fcp_io_channel = phba->sli4_hba.num_present_cpu;
6218 	if (phba->cfg_nvme_io_channel == 0)
6219 		phba->cfg_nvme_io_channel = phba->sli4_hba.num_present_cpu;
6220 
6221 	if (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
6222 		phba->cfg_fcp_io_channel = 0;
6223 
6224 	if (phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP)
6225 		phba->cfg_nvme_io_channel = 0;
6226 
6227 	if (phba->cfg_fcp_io_channel > phba->cfg_nvme_io_channel)
6228 		phba->io_channel_irqs = phba->cfg_fcp_io_channel;
6229 	else
6230 		phba->io_channel_irqs = phba->cfg_nvme_io_channel;
6231 
6232 	phba->cfg_soft_wwnn = 0L;
6233 	phba->cfg_soft_wwpn = 0L;
6234 	lpfc_xri_split_init(phba, lpfc_xri_split);
6235 	lpfc_sg_seg_cnt_init(phba, lpfc_sg_seg_cnt);
6236 	lpfc_hba_queue_depth_init(phba, lpfc_hba_queue_depth);
6237 	lpfc_hba_log_verbose_init(phba, lpfc_log_verbose);
6238 	lpfc_aer_support_init(phba, lpfc_aer_support);
6239 	lpfc_sriov_nr_virtfn_init(phba, lpfc_sriov_nr_virtfn);
6240 	lpfc_request_firmware_upgrade_init(phba, lpfc_req_fw_upgrade);
6241 	lpfc_suppress_link_up_init(phba, lpfc_suppress_link_up);
6242 	lpfc_iocb_cnt_init(phba, lpfc_iocb_cnt);
6243 	lpfc_delay_discovery_init(phba, lpfc_delay_discovery);
6244 	lpfc_sli_mode_init(phba, lpfc_sli_mode);
6245 	phba->cfg_enable_dss = 1;
6246 	lpfc_enable_mds_diags_init(phba, lpfc_enable_mds_diags);
6247 	return;
6248 }
6249 
6250 /**
6251  * lpfc_nvme_mod_param_dep - Adjust module parameter value based on
6252  * dependencies between protocols and roles.
6253  * @phba: lpfc_hba pointer.
6254  **/
6255 void
6256 lpfc_nvme_mod_param_dep(struct lpfc_hba *phba)
6257 {
6258 	if (phba->cfg_nvme_io_channel > phba->sli4_hba.num_present_cpu)
6259 		phba->cfg_nvme_io_channel = phba->sli4_hba.num_present_cpu;
6260 
6261 	if (phba->cfg_fcp_io_channel > phba->sli4_hba.num_present_cpu)
6262 		phba->cfg_fcp_io_channel = phba->sli4_hba.num_present_cpu;
6263 
6264 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME &&
6265 	    phba->nvmet_support) {
6266 		phba->cfg_enable_fc4_type &= ~LPFC_ENABLE_FCP;
6267 		phba->cfg_fcp_io_channel = 0;
6268 
6269 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
6270 				"6013 %s x%x fb_size x%x, fb_max x%x\n",
6271 				"NVME Target PRLI ACC enable_fb ",
6272 				phba->cfg_nvme_enable_fb,
6273 				phba->cfg_nvmet_fb_size,
6274 				LPFC_NVMET_FB_SZ_MAX);
6275 
6276 		if (phba->cfg_nvme_enable_fb == 0)
6277 			phba->cfg_nvmet_fb_size = 0;
6278 		else {
6279 			if (phba->cfg_nvmet_fb_size > LPFC_NVMET_FB_SZ_MAX)
6280 				phba->cfg_nvmet_fb_size = LPFC_NVMET_FB_SZ_MAX;
6281 		}
6282 
6283 		/* Adjust lpfc_nvmet_mrq to avoid running out of WQE slots */
6284 		if (phba->cfg_nvmet_mrq > phba->cfg_nvme_io_channel) {
6285 			phba->cfg_nvmet_mrq = phba->cfg_nvme_io_channel;
6286 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
6287 					"6018 Adjust lpfc_nvmet_mrq to %d\n",
6288 					phba->cfg_nvmet_mrq);
6289 		}
6290 	} else {
6291 		/* Not NVME Target mode.  Turn off Target parameters. */
6292 		phba->nvmet_support = 0;
6293 		phba->cfg_nvmet_mrq = 0;
6294 		phba->cfg_nvmet_fb_size = 0;
6295 	}
6296 
6297 	if (phba->cfg_fcp_io_channel > phba->cfg_nvme_io_channel)
6298 		phba->io_channel_irqs = phba->cfg_fcp_io_channel;
6299 	else
6300 		phba->io_channel_irqs = phba->cfg_nvme_io_channel;
6301 }
6302 
6303 /**
6304  * lpfc_get_vport_cfgparam - Used during port create, init the vport structure
6305  * @vport: lpfc_vport pointer.
6306  **/
6307 void
6308 lpfc_get_vport_cfgparam(struct lpfc_vport *vport)
6309 {
6310 	lpfc_log_verbose_init(vport, lpfc_log_verbose);
6311 	lpfc_lun_queue_depth_init(vport, lpfc_lun_queue_depth);
6312 	lpfc_tgt_queue_depth_init(vport, lpfc_tgt_queue_depth);
6313 	lpfc_devloss_tmo_init(vport, lpfc_devloss_tmo);
6314 	lpfc_nodev_tmo_init(vport, lpfc_nodev_tmo);
6315 	lpfc_peer_port_login_init(vport, lpfc_peer_port_login);
6316 	lpfc_restrict_login_init(vport, lpfc_restrict_login);
6317 	lpfc_fcp_class_init(vport, lpfc_fcp_class);
6318 	lpfc_use_adisc_init(vport, lpfc_use_adisc);
6319 	lpfc_first_burst_size_init(vport, lpfc_first_burst_size);
6320 	lpfc_max_scsicmpl_time_init(vport, lpfc_max_scsicmpl_time);
6321 	lpfc_discovery_threads_init(vport, lpfc_discovery_threads);
6322 	lpfc_max_luns_init(vport, lpfc_max_luns);
6323 	lpfc_scan_down_init(vport, lpfc_scan_down);
6324 	lpfc_enable_da_id_init(vport, lpfc_enable_da_id);
6325 	return;
6326 }
6327