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