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