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