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