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