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