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