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