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