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