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