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