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