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