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