]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/scsi/lpfc/lpfc_attr.c
NFS: use correct fs type for v4 submounts and referrals
[linux-2.6-omap-h63xx.git] / drivers / scsi / lpfc / lpfc_attr.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2008 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_hw.h"
34 #include "lpfc_sli.h"
35 #include "lpfc_disc.h"
36 #include "lpfc_scsi.h"
37 #include "lpfc.h"
38 #include "lpfc_logmsg.h"
39 #include "lpfc_version.h"
40 #include "lpfc_compat.h"
41 #include "lpfc_crtn.h"
42 #include "lpfc_vport.h"
43
44 #define LPFC_DEF_DEVLOSS_TMO 30
45 #define LPFC_MIN_DEVLOSS_TMO 1
46 #define LPFC_MAX_DEVLOSS_TMO 255
47
48 #define LPFC_MAX_LINK_SPEED 8
49 #define LPFC_LINK_SPEED_BITMAP 0x00000117
50 #define LPFC_LINK_SPEED_STRING "0, 1, 2, 4, 8"
51
52 static void
53 lpfc_jedec_to_ascii(int incr, char hdw[])
54 {
55         int i, j;
56         for (i = 0; i < 8; i++) {
57                 j = (incr & 0xf);
58                 if (j <= 9)
59                         hdw[7 - i] = 0x30 +  j;
60                  else
61                         hdw[7 - i] = 0x61 + j - 10;
62                 incr = (incr >> 4);
63         }
64         hdw[8] = 0;
65         return;
66 }
67
68 static ssize_t
69 lpfc_drvr_version_show(struct device *dev, struct device_attribute *attr,
70                        char *buf)
71 {
72         return snprintf(buf, PAGE_SIZE, LPFC_MODULE_DESC "\n");
73 }
74
75 static ssize_t
76 lpfc_info_show(struct device *dev, struct device_attribute *attr,
77                char *buf)
78 {
79         struct Scsi_Host *host = class_to_shost(dev);
80
81         return snprintf(buf, PAGE_SIZE, "%s\n",lpfc_info(host));
82 }
83
84 static ssize_t
85 lpfc_serialnum_show(struct device *dev, struct device_attribute *attr,
86                     char *buf)
87 {
88         struct Scsi_Host  *shost = class_to_shost(dev);
89         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
90         struct lpfc_hba   *phba = vport->phba;
91
92         return snprintf(buf, PAGE_SIZE, "%s\n",phba->SerialNumber);
93 }
94
95 static ssize_t
96 lpfc_temp_sensor_show(struct device *dev, struct device_attribute *attr,
97                       char *buf)
98 {
99         struct Scsi_Host *shost = class_to_shost(dev);
100         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
101         struct lpfc_hba   *phba = vport->phba;
102         return snprintf(buf, PAGE_SIZE, "%d\n",phba->temp_sensor_support);
103 }
104
105 static ssize_t
106 lpfc_modeldesc_show(struct device *dev, struct device_attribute *attr,
107                     char *buf)
108 {
109         struct Scsi_Host  *shost = class_to_shost(dev);
110         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
111         struct lpfc_hba   *phba = vport->phba;
112
113         return snprintf(buf, PAGE_SIZE, "%s\n",phba->ModelDesc);
114 }
115
116 static ssize_t
117 lpfc_modelname_show(struct device *dev, struct device_attribute *attr,
118                     char *buf)
119 {
120         struct Scsi_Host  *shost = class_to_shost(dev);
121         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
122         struct lpfc_hba   *phba = vport->phba;
123
124         return snprintf(buf, PAGE_SIZE, "%s\n",phba->ModelName);
125 }
126
127 static ssize_t
128 lpfc_programtype_show(struct device *dev, struct device_attribute *attr,
129                       char *buf)
130 {
131         struct Scsi_Host  *shost = class_to_shost(dev);
132         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
133         struct lpfc_hba   *phba = vport->phba;
134
135         return snprintf(buf, PAGE_SIZE, "%s\n",phba->ProgramType);
136 }
137
138 static ssize_t
139 lpfc_vportnum_show(struct device *dev, struct device_attribute *attr,
140                    char *buf)
141 {
142         struct Scsi_Host  *shost = class_to_shost(dev);
143         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
144         struct lpfc_hba   *phba = vport->phba;
145
146         return snprintf(buf, PAGE_SIZE, "%s\n",phba->Port);
147 }
148
149 static ssize_t
150 lpfc_fwrev_show(struct device *dev, struct device_attribute *attr,
151                 char *buf)
152 {
153         struct Scsi_Host  *shost = class_to_shost(dev);
154         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
155         struct lpfc_hba   *phba = vport->phba;
156         char fwrev[32];
157
158         lpfc_decode_firmware_rev(phba, fwrev, 1);
159         return snprintf(buf, PAGE_SIZE, "%s, sli-%d\n", fwrev, phba->sli_rev);
160 }
161
162 static ssize_t
163 lpfc_hdw_show(struct device *dev, struct device_attribute *attr, char *buf)
164 {
165         char hdw[9];
166         struct Scsi_Host  *shost = class_to_shost(dev);
167         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
168         struct lpfc_hba   *phba = vport->phba;
169         lpfc_vpd_t *vp = &phba->vpd;
170
171         lpfc_jedec_to_ascii(vp->rev.biuRev, hdw);
172         return snprintf(buf, PAGE_SIZE, "%s\n", hdw);
173 }
174 static ssize_t
175 lpfc_option_rom_version_show(struct device *dev, struct device_attribute *attr,
176                              char *buf)
177 {
178         struct Scsi_Host  *shost = class_to_shost(dev);
179         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
180         struct lpfc_hba   *phba = vport->phba;
181
182         return snprintf(buf, PAGE_SIZE, "%s\n", phba->OptionROMVersion);
183 }
184 static ssize_t
185 lpfc_link_state_show(struct device *dev, struct device_attribute *attr,
186                      char *buf)
187 {
188         struct Scsi_Host  *shost = class_to_shost(dev);
189         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
190         struct lpfc_hba   *phba = vport->phba;
191         int  len = 0;
192
193         switch (phba->link_state) {
194         case LPFC_LINK_UNKNOWN:
195         case LPFC_WARM_START:
196         case LPFC_INIT_START:
197         case LPFC_INIT_MBX_CMDS:
198         case LPFC_LINK_DOWN:
199         case LPFC_HBA_ERROR:
200                 len += snprintf(buf + len, PAGE_SIZE-len, "Link Down\n");
201                 break;
202         case LPFC_LINK_UP:
203         case LPFC_CLEAR_LA:
204         case LPFC_HBA_READY:
205                 len += snprintf(buf + len, PAGE_SIZE-len, "Link Up - ");
206
207                 switch (vport->port_state) {
208                 case LPFC_LOCAL_CFG_LINK:
209                         len += snprintf(buf + len, PAGE_SIZE-len,
210                                         "Configuring Link\n");
211                         break;
212                 case LPFC_FDISC:
213                 case LPFC_FLOGI:
214                 case LPFC_FABRIC_CFG_LINK:
215                 case LPFC_NS_REG:
216                 case LPFC_NS_QRY:
217                 case LPFC_BUILD_DISC_LIST:
218                 case LPFC_DISC_AUTH:
219                         len += snprintf(buf + len, PAGE_SIZE - len,
220                                         "Discovery\n");
221                         break;
222                 case LPFC_VPORT_READY:
223                         len += snprintf(buf + len, PAGE_SIZE - len, "Ready\n");
224                         break;
225
226                 case LPFC_VPORT_FAILED:
227                         len += snprintf(buf + len, PAGE_SIZE - len, "Failed\n");
228                         break;
229
230                 case LPFC_VPORT_UNKNOWN:
231                         len += snprintf(buf + len, PAGE_SIZE - len,
232                                         "Unknown\n");
233                         break;
234                 }
235
236                 if (phba->fc_topology == TOPOLOGY_LOOP) {
237                         if (vport->fc_flag & FC_PUBLIC_LOOP)
238                                 len += snprintf(buf + len, PAGE_SIZE-len,
239                                                 "   Public Loop\n");
240                         else
241                                 len += snprintf(buf + len, PAGE_SIZE-len,
242                                                 "   Private Loop\n");
243                 } else {
244                         if (vport->fc_flag & FC_FABRIC)
245                                 len += snprintf(buf + len, PAGE_SIZE-len,
246                                                 "   Fabric\n");
247                         else
248                                 len += snprintf(buf + len, PAGE_SIZE-len,
249                                                 "   Point-2-Point\n");
250                 }
251         }
252
253         return len;
254 }
255
256 static ssize_t
257 lpfc_num_discovered_ports_show(struct device *dev,
258                                struct device_attribute *attr, char *buf)
259 {
260         struct Scsi_Host  *shost = class_to_shost(dev);
261         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
262
263         return snprintf(buf, PAGE_SIZE, "%d\n",
264                         vport->fc_map_cnt + vport->fc_unmap_cnt);
265 }
266
267
268 static int
269 lpfc_issue_lip(struct Scsi_Host *shost)
270 {
271         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
272         struct lpfc_hba   *phba = vport->phba;
273         LPFC_MBOXQ_t *pmboxq;
274         int mbxstatus = MBXERR_ERROR;
275
276         if ((vport->fc_flag & FC_OFFLINE_MODE) ||
277             (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO))
278                 return -EPERM;
279
280         pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
281
282         if (!pmboxq)
283                 return -ENOMEM;
284
285         memset((void *)pmboxq, 0, sizeof (LPFC_MBOXQ_t));
286         pmboxq->mb.mbxCommand = MBX_DOWN_LINK;
287         pmboxq->mb.mbxOwner = OWN_HOST;
288
289         mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO * 2);
290
291         if ((mbxstatus == MBX_SUCCESS) && (pmboxq->mb.mbxStatus == 0)) {
292                 memset((void *)pmboxq, 0, sizeof (LPFC_MBOXQ_t));
293                 lpfc_init_link(phba, pmboxq, phba->cfg_topology,
294                                phba->cfg_link_speed);
295                 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
296                                                      phba->fc_ratov * 2);
297         }
298
299         lpfc_set_loopback_flag(phba);
300         if (mbxstatus != MBX_TIMEOUT)
301                 mempool_free(pmboxq, phba->mbox_mem_pool);
302
303         if (mbxstatus == MBXERR_ERROR)
304                 return -EIO;
305
306         return 0;
307 }
308
309 static int
310 lpfc_do_offline(struct lpfc_hba *phba, uint32_t type)
311 {
312         struct completion online_compl;
313         struct lpfc_sli_ring *pring;
314         struct lpfc_sli *psli;
315         int status = 0;
316         int cnt = 0;
317         int i;
318
319         init_completion(&online_compl);
320         lpfc_workq_post_event(phba, &status, &online_compl,
321                               LPFC_EVT_OFFLINE_PREP);
322         wait_for_completion(&online_compl);
323
324         if (status != 0)
325                 return -EIO;
326
327         psli = &phba->sli;
328
329         /* Wait a little for things to settle down, but not
330          * long enough for dev loss timeout to expire.
331          */
332         for (i = 0; i < psli->num_rings; i++) {
333                 pring = &psli->ring[i];
334                 while (pring->txcmplq_cnt) {
335                         msleep(10);
336                         if (cnt++ > 500) {  /* 5 secs */
337                                 lpfc_printf_log(phba,
338                                         KERN_WARNING, LOG_INIT,
339                                         "0466 Outstanding IO when "
340                                         "bringing Adapter offline\n");
341                                 break;
342                         }
343                 }
344         }
345
346         init_completion(&online_compl);
347         lpfc_workq_post_event(phba, &status, &online_compl, type);
348         wait_for_completion(&online_compl);
349
350         if (status != 0)
351                 return -EIO;
352
353         return 0;
354 }
355
356 static int
357 lpfc_selective_reset(struct lpfc_hba *phba)
358 {
359         struct completion online_compl;
360         int status = 0;
361
362         if (!phba->cfg_enable_hba_reset)
363                 return -EIO;
364
365         status = lpfc_do_offline(phba, LPFC_EVT_OFFLINE);
366
367         if (status != 0)
368                 return status;
369
370         init_completion(&online_compl);
371         lpfc_workq_post_event(phba, &status, &online_compl,
372                               LPFC_EVT_ONLINE);
373         wait_for_completion(&online_compl);
374
375         if (status != 0)
376                 return -EIO;
377
378         return 0;
379 }
380
381 static ssize_t
382 lpfc_issue_reset(struct device *dev, struct device_attribute *attr,
383                  const char *buf, size_t count)
384 {
385         struct Scsi_Host  *shost = class_to_shost(dev);
386         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
387         struct lpfc_hba   *phba = vport->phba;
388
389         int status = -EINVAL;
390
391         if (strncmp(buf, "selective", sizeof("selective") - 1) == 0)
392                 status = lpfc_selective_reset(phba);
393
394         if (status == 0)
395                 return strlen(buf);
396         else
397                 return status;
398 }
399
400 static ssize_t
401 lpfc_nport_evt_cnt_show(struct device *dev, struct device_attribute *attr,
402                         char *buf)
403 {
404         struct Scsi_Host  *shost = class_to_shost(dev);
405         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
406         struct lpfc_hba   *phba = vport->phba;
407
408         return snprintf(buf, PAGE_SIZE, "%d\n", phba->nport_event_cnt);
409 }
410
411 static ssize_t
412 lpfc_board_mode_show(struct device *dev, struct device_attribute *attr,
413                      char *buf)
414 {
415         struct Scsi_Host  *shost = class_to_shost(dev);
416         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
417         struct lpfc_hba   *phba = vport->phba;
418         char  * state;
419
420         if (phba->link_state == LPFC_HBA_ERROR)
421                 state = "error";
422         else if (phba->link_state == LPFC_WARM_START)
423                 state = "warm start";
424         else if (phba->link_state == LPFC_INIT_START)
425                 state = "offline";
426         else
427                 state = "online";
428
429         return snprintf(buf, PAGE_SIZE, "%s\n", state);
430 }
431
432 static ssize_t
433 lpfc_board_mode_store(struct device *dev, struct device_attribute *attr,
434                       const char *buf, size_t count)
435 {
436         struct Scsi_Host  *shost = class_to_shost(dev);
437         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
438         struct lpfc_hba   *phba = vport->phba;
439         struct completion online_compl;
440         int status=0;
441
442         if (!phba->cfg_enable_hba_reset)
443                 return -EACCES;
444         init_completion(&online_compl);
445
446         if(strncmp(buf, "online", sizeof("online") - 1) == 0) {
447                 lpfc_workq_post_event(phba, &status, &online_compl,
448                                       LPFC_EVT_ONLINE);
449                 wait_for_completion(&online_compl);
450         } else if (strncmp(buf, "offline", sizeof("offline") - 1) == 0)
451                 status = lpfc_do_offline(phba, LPFC_EVT_OFFLINE);
452         else if (strncmp(buf, "warm", sizeof("warm") - 1) == 0)
453                 status = lpfc_do_offline(phba, LPFC_EVT_WARM_START);
454         else if (strncmp(buf, "error", sizeof("error") - 1) == 0)
455                 status = lpfc_do_offline(phba, LPFC_EVT_KILL);
456         else
457                 return -EINVAL;
458
459         if (!status)
460                 return strlen(buf);
461         else
462                 return -EIO;
463 }
464
465 static int
466 lpfc_get_hba_info(struct lpfc_hba *phba,
467                   uint32_t *mxri, uint32_t *axri,
468                   uint32_t *mrpi, uint32_t *arpi,
469                   uint32_t *mvpi, uint32_t *avpi)
470 {
471         struct lpfc_sli   *psli = &phba->sli;
472         LPFC_MBOXQ_t *pmboxq;
473         MAILBOX_t *pmb;
474         int rc = 0;
475
476         /*
477          * prevent udev from issuing mailbox commands until the port is
478          * configured.
479          */
480         if (phba->link_state < LPFC_LINK_DOWN ||
481             !phba->mbox_mem_pool ||
482             (phba->sli.sli_flag & LPFC_SLI2_ACTIVE) == 0)
483                 return 0;
484
485         if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO)
486                 return 0;
487
488         pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
489         if (!pmboxq)
490                 return 0;
491         memset(pmboxq, 0, sizeof (LPFC_MBOXQ_t));
492
493         pmb = &pmboxq->mb;
494         pmb->mbxCommand = MBX_READ_CONFIG;
495         pmb->mbxOwner = OWN_HOST;
496         pmboxq->context1 = NULL;
497
498         if ((phba->pport->fc_flag & FC_OFFLINE_MODE) ||
499                 (!(psli->sli_flag & LPFC_SLI2_ACTIVE)))
500                 rc = MBX_NOT_FINISHED;
501         else
502                 rc = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
503
504         if (rc != MBX_SUCCESS) {
505                 if (rc != MBX_TIMEOUT)
506                         mempool_free(pmboxq, phba->mbox_mem_pool);
507                 return 0;
508         }
509
510         if (mrpi)
511                 *mrpi = pmb->un.varRdConfig.max_rpi;
512         if (arpi)
513                 *arpi = pmb->un.varRdConfig.avail_rpi;
514         if (mxri)
515                 *mxri = pmb->un.varRdConfig.max_xri;
516         if (axri)
517                 *axri = pmb->un.varRdConfig.avail_xri;
518         if (mvpi)
519                 *mvpi = pmb->un.varRdConfig.max_vpi;
520         if (avpi)
521                 *avpi = pmb->un.varRdConfig.avail_vpi;
522
523         mempool_free(pmboxq, phba->mbox_mem_pool);
524         return 1;
525 }
526
527 static ssize_t
528 lpfc_max_rpi_show(struct device *dev, struct device_attribute *attr,
529                   char *buf)
530 {
531         struct Scsi_Host  *shost = class_to_shost(dev);
532         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
533         struct lpfc_hba   *phba = vport->phba;
534         uint32_t cnt;
535
536         if (lpfc_get_hba_info(phba, NULL, NULL, &cnt, NULL, NULL, NULL))
537                 return snprintf(buf, PAGE_SIZE, "%d\n", cnt);
538         return snprintf(buf, PAGE_SIZE, "Unknown\n");
539 }
540
541 static ssize_t
542 lpfc_used_rpi_show(struct device *dev, struct device_attribute *attr,
543                    char *buf)
544 {
545         struct Scsi_Host  *shost = class_to_shost(dev);
546         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
547         struct lpfc_hba   *phba = vport->phba;
548         uint32_t cnt, acnt;
549
550         if (lpfc_get_hba_info(phba, NULL, NULL, &cnt, &acnt, NULL, NULL))
551                 return snprintf(buf, PAGE_SIZE, "%d\n", (cnt - acnt));
552         return snprintf(buf, PAGE_SIZE, "Unknown\n");
553 }
554
555 static ssize_t
556 lpfc_max_xri_show(struct device *dev, struct device_attribute *attr,
557                   char *buf)
558 {
559         struct Scsi_Host  *shost = class_to_shost(dev);
560         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
561         struct lpfc_hba   *phba = vport->phba;
562         uint32_t cnt;
563
564         if (lpfc_get_hba_info(phba, &cnt, NULL, NULL, NULL, NULL, NULL))
565                 return snprintf(buf, PAGE_SIZE, "%d\n", cnt);
566         return snprintf(buf, PAGE_SIZE, "Unknown\n");
567 }
568
569 static ssize_t
570 lpfc_used_xri_show(struct device *dev, struct device_attribute *attr,
571                    char *buf)
572 {
573         struct Scsi_Host  *shost = class_to_shost(dev);
574         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
575         struct lpfc_hba   *phba = vport->phba;
576         uint32_t cnt, acnt;
577
578         if (lpfc_get_hba_info(phba, &cnt, &acnt, NULL, NULL, NULL, NULL))
579                 return snprintf(buf, PAGE_SIZE, "%d\n", (cnt - acnt));
580         return snprintf(buf, PAGE_SIZE, "Unknown\n");
581 }
582
583 static ssize_t
584 lpfc_max_vpi_show(struct device *dev, struct device_attribute *attr,
585                   char *buf)
586 {
587         struct Scsi_Host  *shost = class_to_shost(dev);
588         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
589         struct lpfc_hba   *phba = vport->phba;
590         uint32_t cnt;
591
592         if (lpfc_get_hba_info(phba, NULL, NULL, NULL, NULL, &cnt, NULL))
593                 return snprintf(buf, PAGE_SIZE, "%d\n", cnt);
594         return snprintf(buf, PAGE_SIZE, "Unknown\n");
595 }
596
597 static ssize_t
598 lpfc_used_vpi_show(struct device *dev, struct device_attribute *attr,
599                    char *buf)
600 {
601         struct Scsi_Host  *shost = class_to_shost(dev);
602         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
603         struct lpfc_hba   *phba = vport->phba;
604         uint32_t cnt, acnt;
605
606         if (lpfc_get_hba_info(phba, NULL, NULL, NULL, NULL, &cnt, &acnt))
607                 return snprintf(buf, PAGE_SIZE, "%d\n", (cnt - acnt));
608         return snprintf(buf, PAGE_SIZE, "Unknown\n");
609 }
610
611 static ssize_t
612 lpfc_npiv_info_show(struct device *dev, struct device_attribute *attr,
613                     char *buf)
614 {
615         struct Scsi_Host  *shost = class_to_shost(dev);
616         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
617         struct lpfc_hba   *phba = vport->phba;
618
619         if (!(phba->max_vpi))
620                 return snprintf(buf, PAGE_SIZE, "NPIV Not Supported\n");
621         if (vport->port_type == LPFC_PHYSICAL_PORT)
622                 return snprintf(buf, PAGE_SIZE, "NPIV Physical\n");
623         return snprintf(buf, PAGE_SIZE, "NPIV Virtual (VPI %d)\n", vport->vpi);
624 }
625
626 static ssize_t
627 lpfc_poll_show(struct device *dev, struct device_attribute *attr,
628                char *buf)
629 {
630         struct Scsi_Host  *shost = class_to_shost(dev);
631         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
632         struct lpfc_hba   *phba = vport->phba;
633
634         return snprintf(buf, PAGE_SIZE, "%#x\n", phba->cfg_poll);
635 }
636
637 static ssize_t
638 lpfc_poll_store(struct device *dev, struct device_attribute *attr,
639                 const char *buf, size_t count)
640 {
641         struct Scsi_Host  *shost = class_to_shost(dev);
642         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
643         struct lpfc_hba   *phba = vport->phba;
644         uint32_t creg_val;
645         uint32_t old_val;
646         int val=0;
647
648         if (!isdigit(buf[0]))
649                 return -EINVAL;
650
651         if (sscanf(buf, "%i", &val) != 1)
652                 return -EINVAL;
653
654         if ((val & 0x3) != val)
655                 return -EINVAL;
656
657         spin_lock_irq(&phba->hbalock);
658
659         old_val = phba->cfg_poll;
660
661         if (val & ENABLE_FCP_RING_POLLING) {
662                 if ((val & DISABLE_FCP_RING_INT) &&
663                     !(old_val & DISABLE_FCP_RING_INT)) {
664                         creg_val = readl(phba->HCregaddr);
665                         creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
666                         writel(creg_val, phba->HCregaddr);
667                         readl(phba->HCregaddr); /* flush */
668
669                         lpfc_poll_start_timer(phba);
670                 }
671         } else if (val != 0x0) {
672                 spin_unlock_irq(&phba->hbalock);
673                 return -EINVAL;
674         }
675
676         if (!(val & DISABLE_FCP_RING_INT) &&
677             (old_val & DISABLE_FCP_RING_INT))
678         {
679                 spin_unlock_irq(&phba->hbalock);
680                 del_timer(&phba->fcp_poll_timer);
681                 spin_lock_irq(&phba->hbalock);
682                 creg_val = readl(phba->HCregaddr);
683                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
684                 writel(creg_val, phba->HCregaddr);
685                 readl(phba->HCregaddr); /* flush */
686         }
687
688         phba->cfg_poll = val;
689
690         spin_unlock_irq(&phba->hbalock);
691
692         return strlen(buf);
693 }
694
695 #define lpfc_param_show(attr)   \
696 static ssize_t \
697 lpfc_##attr##_show(struct device *dev, struct device_attribute *attr, \
698                    char *buf) \
699 { \
700         struct Scsi_Host  *shost = class_to_shost(dev);\
701         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;\
702         struct lpfc_hba   *phba = vport->phba;\
703         int val = 0;\
704         val = phba->cfg_##attr;\
705         return snprintf(buf, PAGE_SIZE, "%d\n",\
706                         phba->cfg_##attr);\
707 }
708
709 #define lpfc_param_hex_show(attr)       \
710 static ssize_t \
711 lpfc_##attr##_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         int val = 0;\
718         val = phba->cfg_##attr;\
719         return snprintf(buf, PAGE_SIZE, "%#x\n",\
720                         phba->cfg_##attr);\
721 }
722
723 #define lpfc_param_init(attr, default, minval, maxval)  \
724 static int \
725 lpfc_##attr##_init(struct lpfc_hba *phba, int val) \
726 { \
727         if (val >= minval && val <= maxval) {\
728                 phba->cfg_##attr = val;\
729                 return 0;\
730         }\
731         lpfc_printf_log(phba, KERN_ERR, LOG_INIT, \
732                         "0449 lpfc_"#attr" attribute cannot be set to %d, "\
733                         "allowed range is ["#minval", "#maxval"]\n", val); \
734         phba->cfg_##attr = default;\
735         return -EINVAL;\
736 }
737
738 #define lpfc_param_set(attr, default, minval, maxval)   \
739 static int \
740 lpfc_##attr##_set(struct lpfc_hba *phba, int val) \
741 { \
742         if (val >= minval && val <= maxval) {\
743                 phba->cfg_##attr = val;\
744                 return 0;\
745         }\
746         lpfc_printf_log(phba, KERN_ERR, LOG_INIT, \
747                         "0450 lpfc_"#attr" attribute cannot be set to %d, "\
748                         "allowed range is ["#minval", "#maxval"]\n", val); \
749         return -EINVAL;\
750 }
751
752 #define lpfc_param_store(attr)  \
753 static ssize_t \
754 lpfc_##attr##_store(struct device *dev, struct device_attribute *attr, \
755                     const char *buf, size_t count) \
756 { \
757         struct Scsi_Host  *shost = class_to_shost(dev);\
758         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;\
759         struct lpfc_hba   *phba = vport->phba;\
760         int val=0;\
761         if (!isdigit(buf[0]))\
762                 return -EINVAL;\
763         if (sscanf(buf, "%i", &val) != 1)\
764                 return -EINVAL;\
765         if (lpfc_##attr##_set(phba, val) == 0) \
766                 return strlen(buf);\
767         else \
768                 return -EINVAL;\
769 }
770
771 #define lpfc_vport_param_show(attr)     \
772 static ssize_t \
773 lpfc_##attr##_show(struct device *dev, struct device_attribute *attr, \
774                    char *buf) \
775 { \
776         struct Scsi_Host  *shost = class_to_shost(dev);\
777         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;\
778         int val = 0;\
779         val = vport->cfg_##attr;\
780         return snprintf(buf, PAGE_SIZE, "%d\n", vport->cfg_##attr);\
781 }
782
783 #define lpfc_vport_param_hex_show(attr) \
784 static ssize_t \
785 lpfc_##attr##_show(struct device *dev, struct device_attribute *attr, \
786                    char *buf) \
787 { \
788         struct Scsi_Host  *shost = class_to_shost(dev);\
789         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;\
790         int val = 0;\
791         val = vport->cfg_##attr;\
792         return snprintf(buf, PAGE_SIZE, "%#x\n", vport->cfg_##attr);\
793 }
794
795 #define lpfc_vport_param_init(attr, default, minval, maxval)    \
796 static int \
797 lpfc_##attr##_init(struct lpfc_vport *vport, int val) \
798 { \
799         if (val >= minval && val <= maxval) {\
800                 vport->cfg_##attr = val;\
801                 return 0;\
802         }\
803         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT, \
804                          "0449 lpfc_"#attr" attribute cannot be set to %d, "\
805                          "allowed range is ["#minval", "#maxval"]\n", val); \
806         vport->cfg_##attr = default;\
807         return -EINVAL;\
808 }
809
810 #define lpfc_vport_param_set(attr, default, minval, maxval)     \
811 static int \
812 lpfc_##attr##_set(struct lpfc_vport *vport, int val) \
813 { \
814         if (val >= minval && val <= maxval) {\
815                 vport->cfg_##attr = val;\
816                 return 0;\
817         }\
818         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT, \
819                          "0450 lpfc_"#attr" attribute cannot be set to %d, "\
820                          "allowed range is ["#minval", "#maxval"]\n", val); \
821         return -EINVAL;\
822 }
823
824 #define lpfc_vport_param_store(attr)    \
825 static ssize_t \
826 lpfc_##attr##_store(struct device *dev, struct device_attribute *attr, \
827                     const char *buf, size_t count) \
828 { \
829         struct Scsi_Host  *shost = class_to_shost(dev);\
830         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;\
831         int val=0;\
832         if (!isdigit(buf[0]))\
833                 return -EINVAL;\
834         if (sscanf(buf, "%i", &val) != 1)\
835                 return -EINVAL;\
836         if (lpfc_##attr##_set(vport, val) == 0) \
837                 return strlen(buf);\
838         else \
839                 return -EINVAL;\
840 }
841
842
843 #define LPFC_ATTR(name, defval, minval, maxval, desc) \
844 static int lpfc_##name = defval;\
845 module_param(lpfc_##name, int, 0);\
846 MODULE_PARM_DESC(lpfc_##name, desc);\
847 lpfc_param_init(name, defval, minval, maxval)
848
849 #define LPFC_ATTR_R(name, defval, minval, maxval, desc) \
850 static int lpfc_##name = defval;\
851 module_param(lpfc_##name, int, 0);\
852 MODULE_PARM_DESC(lpfc_##name, desc);\
853 lpfc_param_show(name)\
854 lpfc_param_init(name, defval, minval, maxval)\
855 static DEVICE_ATTR(lpfc_##name, S_IRUGO , lpfc_##name##_show, NULL)
856
857 #define LPFC_ATTR_RW(name, defval, minval, maxval, desc) \
858 static int lpfc_##name = defval;\
859 module_param(lpfc_##name, int, 0);\
860 MODULE_PARM_DESC(lpfc_##name, desc);\
861 lpfc_param_show(name)\
862 lpfc_param_init(name, defval, minval, maxval)\
863 lpfc_param_set(name, defval, minval, maxval)\
864 lpfc_param_store(name)\
865 static DEVICE_ATTR(lpfc_##name, S_IRUGO | S_IWUSR,\
866                    lpfc_##name##_show, lpfc_##name##_store)
867
868 #define LPFC_ATTR_HEX_R(name, defval, minval, maxval, desc) \
869 static int lpfc_##name = defval;\
870 module_param(lpfc_##name, int, 0);\
871 MODULE_PARM_DESC(lpfc_##name, desc);\
872 lpfc_param_hex_show(name)\
873 lpfc_param_init(name, defval, minval, maxval)\
874 static DEVICE_ATTR(lpfc_##name, S_IRUGO , lpfc_##name##_show, NULL)
875
876 #define LPFC_ATTR_HEX_RW(name, defval, minval, maxval, desc) \
877 static int lpfc_##name = defval;\
878 module_param(lpfc_##name, int, 0);\
879 MODULE_PARM_DESC(lpfc_##name, desc);\
880 lpfc_param_hex_show(name)\
881 lpfc_param_init(name, defval, minval, maxval)\
882 lpfc_param_set(name, defval, minval, maxval)\
883 lpfc_param_store(name)\
884 static DEVICE_ATTR(lpfc_##name, S_IRUGO | S_IWUSR,\
885                    lpfc_##name##_show, lpfc_##name##_store)
886
887 #define LPFC_VPORT_ATTR(name, defval, minval, maxval, desc) \
888 static int lpfc_##name = defval;\
889 module_param(lpfc_##name, int, 0);\
890 MODULE_PARM_DESC(lpfc_##name, desc);\
891 lpfc_vport_param_init(name, defval, minval, maxval)
892
893 #define LPFC_VPORT_ATTR_R(name, defval, minval, maxval, desc) \
894 static int lpfc_##name = defval;\
895 module_param(lpfc_##name, int, 0);\
896 MODULE_PARM_DESC(lpfc_##name, desc);\
897 lpfc_vport_param_show(name)\
898 lpfc_vport_param_init(name, defval, minval, maxval)\
899 static DEVICE_ATTR(lpfc_##name, S_IRUGO , lpfc_##name##_show, NULL)
900
901 #define LPFC_VPORT_ATTR_RW(name, defval, minval, maxval, desc) \
902 static int lpfc_##name = defval;\
903 module_param(lpfc_##name, int, 0);\
904 MODULE_PARM_DESC(lpfc_##name, desc);\
905 lpfc_vport_param_show(name)\
906 lpfc_vport_param_init(name, defval, minval, maxval)\
907 lpfc_vport_param_set(name, defval, minval, maxval)\
908 lpfc_vport_param_store(name)\
909 static DEVICE_ATTR(lpfc_##name, S_IRUGO | S_IWUSR,\
910                    lpfc_##name##_show, lpfc_##name##_store)
911
912 #define LPFC_VPORT_ATTR_HEX_R(name, defval, minval, maxval, desc) \
913 static int lpfc_##name = defval;\
914 module_param(lpfc_##name, int, 0);\
915 MODULE_PARM_DESC(lpfc_##name, desc);\
916 lpfc_vport_param_hex_show(name)\
917 lpfc_vport_param_init(name, defval, minval, maxval)\
918 static DEVICE_ATTR(lpfc_##name, S_IRUGO , lpfc_##name##_show, NULL)
919
920 #define LPFC_VPORT_ATTR_HEX_RW(name, defval, minval, maxval, desc) \
921 static int lpfc_##name = defval;\
922 module_param(lpfc_##name, int, 0);\
923 MODULE_PARM_DESC(lpfc_##name, desc);\
924 lpfc_vport_param_hex_show(name)\
925 lpfc_vport_param_init(name, defval, minval, maxval)\
926 lpfc_vport_param_set(name, defval, minval, maxval)\
927 lpfc_vport_param_store(name)\
928 static DEVICE_ATTR(lpfc_##name, S_IRUGO | S_IWUSR,\
929                    lpfc_##name##_show, lpfc_##name##_store)
930
931 static DEVICE_ATTR(info, S_IRUGO, lpfc_info_show, NULL);
932 static DEVICE_ATTR(serialnum, S_IRUGO, lpfc_serialnum_show, NULL);
933 static DEVICE_ATTR(modeldesc, S_IRUGO, lpfc_modeldesc_show, NULL);
934 static DEVICE_ATTR(modelname, S_IRUGO, lpfc_modelname_show, NULL);
935 static DEVICE_ATTR(programtype, S_IRUGO, lpfc_programtype_show, NULL);
936 static DEVICE_ATTR(portnum, S_IRUGO, lpfc_vportnum_show, NULL);
937 static DEVICE_ATTR(fwrev, S_IRUGO, lpfc_fwrev_show, NULL);
938 static DEVICE_ATTR(hdw, S_IRUGO, lpfc_hdw_show, NULL);
939 static DEVICE_ATTR(link_state, S_IRUGO, lpfc_link_state_show, NULL);
940 static DEVICE_ATTR(option_rom_version, S_IRUGO,
941                    lpfc_option_rom_version_show, NULL);
942 static DEVICE_ATTR(num_discovered_ports, S_IRUGO,
943                    lpfc_num_discovered_ports_show, NULL);
944 static DEVICE_ATTR(nport_evt_cnt, S_IRUGO, lpfc_nport_evt_cnt_show, NULL);
945 static DEVICE_ATTR(lpfc_drvr_version, S_IRUGO, lpfc_drvr_version_show, NULL);
946 static DEVICE_ATTR(board_mode, S_IRUGO | S_IWUSR,
947                    lpfc_board_mode_show, lpfc_board_mode_store);
948 static DEVICE_ATTR(issue_reset, S_IWUSR, NULL, lpfc_issue_reset);
949 static DEVICE_ATTR(max_vpi, S_IRUGO, lpfc_max_vpi_show, NULL);
950 static DEVICE_ATTR(used_vpi, S_IRUGO, lpfc_used_vpi_show, NULL);
951 static DEVICE_ATTR(max_rpi, S_IRUGO, lpfc_max_rpi_show, NULL);
952 static DEVICE_ATTR(used_rpi, S_IRUGO, lpfc_used_rpi_show, NULL);
953 static DEVICE_ATTR(max_xri, S_IRUGO, lpfc_max_xri_show, NULL);
954 static DEVICE_ATTR(used_xri, S_IRUGO, lpfc_used_xri_show, NULL);
955 static DEVICE_ATTR(npiv_info, S_IRUGO, lpfc_npiv_info_show, NULL);
956 static DEVICE_ATTR(lpfc_temp_sensor, S_IRUGO, lpfc_temp_sensor_show, NULL);
957
958
959 static char *lpfc_soft_wwn_key = "C99G71SL8032A";
960
961 static ssize_t
962 lpfc_soft_wwn_enable_store(struct device *dev, struct device_attribute *attr,
963                            const char *buf, size_t count)
964 {
965         struct Scsi_Host  *shost = class_to_shost(dev);
966         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
967         struct lpfc_hba   *phba = vport->phba;
968         unsigned int cnt = count;
969
970         /*
971          * We're doing a simple sanity check for soft_wwpn setting.
972          * We require that the user write a specific key to enable
973          * the soft_wwpn attribute to be settable. Once the attribute
974          * is written, the enable key resets. If further updates are
975          * desired, the key must be written again to re-enable the
976          * attribute.
977          *
978          * The "key" is not secret - it is a hardcoded string shown
979          * here. The intent is to protect against the random user or
980          * application that is just writing attributes.
981          */
982
983         /* count may include a LF at end of string */
984         if (buf[cnt-1] == '\n')
985                 cnt--;
986
987         if ((cnt != strlen(lpfc_soft_wwn_key)) ||
988             (strncmp(buf, lpfc_soft_wwn_key, strlen(lpfc_soft_wwn_key)) != 0))
989                 return -EINVAL;
990
991         phba->soft_wwn_enable = 1;
992         return count;
993 }
994 static DEVICE_ATTR(lpfc_soft_wwn_enable, S_IWUSR, NULL,
995                    lpfc_soft_wwn_enable_store);
996
997 static ssize_t
998 lpfc_soft_wwpn_show(struct device *dev, struct device_attribute *attr,
999                     char *buf)
1000 {
1001         struct Scsi_Host  *shost = class_to_shost(dev);
1002         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1003         struct lpfc_hba   *phba = vport->phba;
1004
1005         return snprintf(buf, PAGE_SIZE, "0x%llx\n",
1006                         (unsigned long long)phba->cfg_soft_wwpn);
1007 }
1008
1009
1010 static ssize_t
1011 lpfc_soft_wwpn_store(struct device *dev, struct device_attribute *attr,
1012                      const char *buf, size_t count)
1013 {
1014         struct Scsi_Host  *shost = class_to_shost(dev);
1015         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1016         struct lpfc_hba   *phba = vport->phba;
1017         struct completion online_compl;
1018         int stat1=0, stat2=0;
1019         unsigned int i, j, cnt=count;
1020         u8 wwpn[8];
1021
1022         if (!phba->cfg_enable_hba_reset)
1023                 return -EACCES;
1024         spin_lock_irq(&phba->hbalock);
1025         if (phba->over_temp_state == HBA_OVER_TEMP) {
1026                 spin_unlock_irq(&phba->hbalock);
1027                 return -EACCES;
1028         }
1029         spin_unlock_irq(&phba->hbalock);
1030         /* count may include a LF at end of string */
1031         if (buf[cnt-1] == '\n')
1032                 cnt--;
1033
1034         if (!phba->soft_wwn_enable || (cnt < 16) || (cnt > 18) ||
1035             ((cnt == 17) && (*buf++ != 'x')) ||
1036             ((cnt == 18) && ((*buf++ != '0') || (*buf++ != 'x'))))
1037                 return -EINVAL;
1038
1039         phba->soft_wwn_enable = 0;
1040
1041         memset(wwpn, 0, sizeof(wwpn));
1042
1043         /* Validate and store the new name */
1044         for (i=0, j=0; i < 16; i++) {
1045                 if ((*buf >= 'a') && (*buf <= 'f'))
1046                         j = ((j << 4) | ((*buf++ -'a') + 10));
1047                 else if ((*buf >= 'A') && (*buf <= 'F'))
1048                         j = ((j << 4) | ((*buf++ -'A') + 10));
1049                 else if ((*buf >= '0') && (*buf <= '9'))
1050                         j = ((j << 4) | (*buf++ -'0'));
1051                 else
1052                         return -EINVAL;
1053                 if (i % 2) {
1054                         wwpn[i/2] = j & 0xff;
1055                         j = 0;
1056                 }
1057         }
1058         phba->cfg_soft_wwpn = wwn_to_u64(wwpn);
1059         fc_host_port_name(shost) = phba->cfg_soft_wwpn;
1060         if (phba->cfg_soft_wwnn)
1061                 fc_host_node_name(shost) = phba->cfg_soft_wwnn;
1062
1063         dev_printk(KERN_NOTICE, &phba->pcidev->dev,
1064                    "lpfc%d: Reinitializing to use soft_wwpn\n", phba->brd_no);
1065
1066         stat1 = lpfc_do_offline(phba, LPFC_EVT_OFFLINE);
1067         if (stat1)
1068                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1069                                 "0463 lpfc_soft_wwpn attribute set failed to "
1070                                 "reinit adapter - %d\n", stat1);
1071         init_completion(&online_compl);
1072         lpfc_workq_post_event(phba, &stat2, &online_compl, LPFC_EVT_ONLINE);
1073         wait_for_completion(&online_compl);
1074         if (stat2)
1075                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1076                                 "0464 lpfc_soft_wwpn attribute set failed to "
1077                                 "reinit adapter - %d\n", stat2);
1078         return (stat1 || stat2) ? -EIO : count;
1079 }
1080 static DEVICE_ATTR(lpfc_soft_wwpn, S_IRUGO | S_IWUSR,\
1081                    lpfc_soft_wwpn_show, lpfc_soft_wwpn_store);
1082
1083 static ssize_t
1084 lpfc_soft_wwnn_show(struct device *dev, struct device_attribute *attr,
1085                     char *buf)
1086 {
1087         struct Scsi_Host *shost = class_to_shost(dev);
1088         struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
1089         return snprintf(buf, PAGE_SIZE, "0x%llx\n",
1090                         (unsigned long long)phba->cfg_soft_wwnn);
1091 }
1092
1093
1094 static ssize_t
1095 lpfc_soft_wwnn_store(struct device *dev, struct device_attribute *attr,
1096                      const char *buf, size_t count)
1097 {
1098         struct Scsi_Host *shost = class_to_shost(dev);
1099         struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
1100         unsigned int i, j, cnt=count;
1101         u8 wwnn[8];
1102
1103         /* count may include a LF at end of string */
1104         if (buf[cnt-1] == '\n')
1105                 cnt--;
1106
1107         if (!phba->soft_wwn_enable || (cnt < 16) || (cnt > 18) ||
1108             ((cnt == 17) && (*buf++ != 'x')) ||
1109             ((cnt == 18) && ((*buf++ != '0') || (*buf++ != 'x'))))
1110                 return -EINVAL;
1111
1112         /*
1113          * Allow wwnn to be set many times, as long as the enable is set.
1114          * However, once the wwpn is set, everything locks.
1115          */
1116
1117         memset(wwnn, 0, sizeof(wwnn));
1118
1119         /* Validate and store the new name */
1120         for (i=0, j=0; i < 16; i++) {
1121                 if ((*buf >= 'a') && (*buf <= 'f'))
1122                         j = ((j << 4) | ((*buf++ -'a') + 10));
1123                 else if ((*buf >= 'A') && (*buf <= 'F'))
1124                         j = ((j << 4) | ((*buf++ -'A') + 10));
1125                 else if ((*buf >= '0') && (*buf <= '9'))
1126                         j = ((j << 4) | (*buf++ -'0'));
1127                 else
1128                         return -EINVAL;
1129                 if (i % 2) {
1130                         wwnn[i/2] = j & 0xff;
1131                         j = 0;
1132                 }
1133         }
1134         phba->cfg_soft_wwnn = wwn_to_u64(wwnn);
1135
1136         dev_printk(KERN_NOTICE, &phba->pcidev->dev,
1137                    "lpfc%d: soft_wwnn set. Value will take effect upon "
1138                    "setting of the soft_wwpn\n", phba->brd_no);
1139
1140         return count;
1141 }
1142 static DEVICE_ATTR(lpfc_soft_wwnn, S_IRUGO | S_IWUSR,\
1143                    lpfc_soft_wwnn_show, lpfc_soft_wwnn_store);
1144
1145
1146 static int lpfc_poll = 0;
1147 module_param(lpfc_poll, int, 0);
1148 MODULE_PARM_DESC(lpfc_poll, "FCP ring polling mode control:"
1149                  " 0 - none,"
1150                  " 1 - poll with interrupts enabled"
1151                  " 3 - poll and disable FCP ring interrupts");
1152
1153 static DEVICE_ATTR(lpfc_poll, S_IRUGO | S_IWUSR,
1154                    lpfc_poll_show, lpfc_poll_store);
1155
1156 int  lpfc_sli_mode = 0;
1157 module_param(lpfc_sli_mode, int, 0);
1158 MODULE_PARM_DESC(lpfc_sli_mode, "SLI mode selector:"
1159                  " 0 - auto (SLI-3 if supported),"
1160                  " 2 - select SLI-2 even on SLI-3 capable HBAs,"
1161                  " 3 - select SLI-3");
1162
1163 int lpfc_enable_npiv = 0;
1164 module_param(lpfc_enable_npiv, int, 0);
1165 MODULE_PARM_DESC(lpfc_enable_npiv, "Enable NPIV functionality");
1166 lpfc_param_show(enable_npiv);
1167 lpfc_param_init(enable_npiv, 0, 0, 1);
1168 static DEVICE_ATTR(lpfc_enable_npiv, S_IRUGO,
1169                          lpfc_enable_npiv_show, NULL);
1170
1171 /*
1172 # lpfc_nodev_tmo: If set, it will hold all I/O errors on devices that disappear
1173 # until the timer expires. Value range is [0,255]. Default value is 30.
1174 */
1175 static int lpfc_nodev_tmo = LPFC_DEF_DEVLOSS_TMO;
1176 static int lpfc_devloss_tmo = LPFC_DEF_DEVLOSS_TMO;
1177 module_param(lpfc_nodev_tmo, int, 0);
1178 MODULE_PARM_DESC(lpfc_nodev_tmo,
1179                  "Seconds driver will hold I/O waiting "
1180                  "for a device to come back");
1181 static ssize_t
1182 lpfc_nodev_tmo_show(struct device *dev, struct device_attribute *attr,
1183                     char *buf)
1184 {
1185         struct Scsi_Host  *shost = class_to_shost(dev);
1186         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1187         int val = 0;
1188         val = vport->cfg_devloss_tmo;
1189         return snprintf(buf, PAGE_SIZE, "%d\n", vport->cfg_devloss_tmo);
1190 }
1191
1192 static int
1193 lpfc_nodev_tmo_init(struct lpfc_vport *vport, int val)
1194 {
1195         if (vport->cfg_devloss_tmo != LPFC_DEF_DEVLOSS_TMO) {
1196                 vport->cfg_nodev_tmo = vport->cfg_devloss_tmo;
1197                 if (val != LPFC_DEF_DEVLOSS_TMO)
1198                         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
1199                                          "0402 Ignoring nodev_tmo module "
1200                                          "parameter because devloss_tmo is "
1201                                          "set.\n");
1202                 return 0;
1203         }
1204
1205         if (val >= LPFC_MIN_DEVLOSS_TMO && val <= LPFC_MAX_DEVLOSS_TMO) {
1206                 vport->cfg_nodev_tmo = val;
1207                 vport->cfg_devloss_tmo = val;
1208                 return 0;
1209         }
1210         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
1211                          "0400 lpfc_nodev_tmo attribute cannot be set to"
1212                          " %d, allowed range is [%d, %d]\n",
1213                          val, LPFC_MIN_DEVLOSS_TMO, LPFC_MAX_DEVLOSS_TMO);
1214         vport->cfg_nodev_tmo = LPFC_DEF_DEVLOSS_TMO;
1215         return -EINVAL;
1216 }
1217
1218 static void
1219 lpfc_update_rport_devloss_tmo(struct lpfc_vport *vport)
1220 {
1221         struct Scsi_Host  *shost;
1222         struct lpfc_nodelist  *ndlp;
1223
1224         shost = lpfc_shost_from_vport(vport);
1225         spin_lock_irq(shost->host_lock);
1226         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp)
1227                 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport)
1228                         ndlp->rport->dev_loss_tmo = vport->cfg_devloss_tmo;
1229         spin_unlock_irq(shost->host_lock);
1230 }
1231
1232 static int
1233 lpfc_nodev_tmo_set(struct lpfc_vport *vport, int val)
1234 {
1235         if (vport->dev_loss_tmo_changed ||
1236             (lpfc_devloss_tmo != LPFC_DEF_DEVLOSS_TMO)) {
1237                 lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
1238                                  "0401 Ignoring change to nodev_tmo "
1239                                  "because devloss_tmo is set.\n");
1240                 return 0;
1241         }
1242         if (val >= LPFC_MIN_DEVLOSS_TMO && val <= LPFC_MAX_DEVLOSS_TMO) {
1243                 vport->cfg_nodev_tmo = val;
1244                 vport->cfg_devloss_tmo = val;
1245                 lpfc_update_rport_devloss_tmo(vport);
1246                 return 0;
1247         }
1248         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
1249                          "0403 lpfc_nodev_tmo attribute cannot be set to"
1250                          "%d, allowed range is [%d, %d]\n",
1251                          val, LPFC_MIN_DEVLOSS_TMO, LPFC_MAX_DEVLOSS_TMO);
1252         return -EINVAL;
1253 }
1254
1255 lpfc_vport_param_store(nodev_tmo)
1256
1257 static DEVICE_ATTR(lpfc_nodev_tmo, S_IRUGO | S_IWUSR,
1258                    lpfc_nodev_tmo_show, lpfc_nodev_tmo_store);
1259
1260 /*
1261 # lpfc_devloss_tmo: If set, it will hold all I/O errors on devices that
1262 # disappear until the timer expires. Value range is [0,255]. Default
1263 # value is 30.
1264 */
1265 module_param(lpfc_devloss_tmo, int, 0);
1266 MODULE_PARM_DESC(lpfc_devloss_tmo,
1267                  "Seconds driver will hold I/O waiting "
1268                  "for a device to come back");
1269 lpfc_vport_param_init(devloss_tmo, LPFC_DEF_DEVLOSS_TMO,
1270                       LPFC_MIN_DEVLOSS_TMO, LPFC_MAX_DEVLOSS_TMO)
1271 lpfc_vport_param_show(devloss_tmo)
1272 static int
1273 lpfc_devloss_tmo_set(struct lpfc_vport *vport, int val)
1274 {
1275         if (val >= LPFC_MIN_DEVLOSS_TMO && val <= LPFC_MAX_DEVLOSS_TMO) {
1276                 vport->cfg_nodev_tmo = val;
1277                 vport->cfg_devloss_tmo = val;
1278                 vport->dev_loss_tmo_changed = 1;
1279                 lpfc_update_rport_devloss_tmo(vport);
1280                 return 0;
1281         }
1282
1283         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
1284                          "0404 lpfc_devloss_tmo attribute cannot be set to"
1285                          " %d, allowed range is [%d, %d]\n",
1286                          val, LPFC_MIN_DEVLOSS_TMO, LPFC_MAX_DEVLOSS_TMO);
1287         return -EINVAL;
1288 }
1289
1290 lpfc_vport_param_store(devloss_tmo)
1291 static DEVICE_ATTR(lpfc_devloss_tmo, S_IRUGO | S_IWUSR,
1292                    lpfc_devloss_tmo_show, lpfc_devloss_tmo_store);
1293
1294 /*
1295 # lpfc_log_verbose: Only turn this flag on if you are willing to risk being
1296 # deluged with LOTS of information.
1297 # You can set a bit mask to record specific types of verbose messages:
1298 #
1299 # LOG_ELS                       0x1        ELS events
1300 # LOG_DISCOVERY                 0x2        Link discovery events
1301 # LOG_MBOX                      0x4        Mailbox events
1302 # LOG_INIT                      0x8        Initialization events
1303 # LOG_LINK_EVENT                0x10       Link events
1304 # LOG_FCP                       0x40       FCP traffic history
1305 # LOG_NODE                      0x80       Node table events
1306 # LOG_MISC                      0x400      Miscellaneous events
1307 # LOG_SLI                       0x800      SLI events
1308 # LOG_FCP_ERROR                 0x1000     Only log FCP errors
1309 # LOG_LIBDFC                    0x2000     LIBDFC events
1310 # LOG_ALL_MSG                   0xffff     LOG all messages
1311 */
1312 LPFC_VPORT_ATTR_HEX_RW(log_verbose, 0x0, 0x0, 0xffff,
1313                        "Verbose logging bit-mask");
1314
1315 /*
1316 # lpfc_enable_da_id: This turns on the DA_ID CT command that deregisters
1317 # objects that have been registered with the nameserver after login.
1318 */
1319 LPFC_VPORT_ATTR_R(enable_da_id, 0, 0, 1,
1320                   "Deregister nameserver objects before LOGO");
1321
1322 /*
1323 # lun_queue_depth:  This parameter is used to limit the number of outstanding
1324 # commands per FCP LUN. Value range is [1,128]. Default value is 30.
1325 */
1326 LPFC_VPORT_ATTR_R(lun_queue_depth, 30, 1, 128,
1327                   "Max number of FCP commands we can queue to a specific LUN");
1328
1329 /*
1330 # hba_queue_depth:  This parameter is used to limit the number of outstanding
1331 # commands per lpfc HBA. Value range is [32,8192]. If this parameter
1332 # value is greater than the maximum number of exchanges supported by the HBA,
1333 # then maximum number of exchanges supported by the HBA is used to determine
1334 # the hba_queue_depth.
1335 */
1336 LPFC_ATTR_R(hba_queue_depth, 8192, 32, 8192,
1337             "Max number of FCP commands we can queue to a lpfc HBA");
1338
1339 /*
1340 # peer_port_login:  This parameter allows/prevents logins
1341 # between peer ports hosted on the same physical port.
1342 # When this parameter is set 0 peer ports of same physical port
1343 # are not allowed to login to each other.
1344 # When this parameter is set 1 peer ports of same physical port
1345 # are allowed to login to each other.
1346 # Default value of this parameter is 0.
1347 */
1348 LPFC_VPORT_ATTR_R(peer_port_login, 0, 0, 1,
1349                   "Allow peer ports on the same physical port to login to each "
1350                   "other.");
1351
1352 /*
1353 # restrict_login:  This parameter allows/prevents logins
1354 # between Virtual Ports and remote initiators.
1355 # When this parameter is not set (0) Virtual Ports will accept PLOGIs from
1356 # other initiators and will attempt to PLOGI all remote ports.
1357 # When this parameter is set (1) Virtual Ports will reject PLOGIs from
1358 # remote ports and will not attempt to PLOGI to other initiators.
1359 # This parameter does not restrict to the physical port.
1360 # This parameter does not restrict logins to Fabric resident remote ports.
1361 # Default value of this parameter is 1.
1362 */
1363 static int lpfc_restrict_login = 1;
1364 module_param(lpfc_restrict_login, int, 0);
1365 MODULE_PARM_DESC(lpfc_restrict_login,
1366                  "Restrict virtual ports login to remote initiators.");
1367 lpfc_vport_param_show(restrict_login);
1368
1369 static int
1370 lpfc_restrict_login_init(struct lpfc_vport *vport, int val)
1371 {
1372         if (val < 0 || val > 1) {
1373                 lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
1374                                  "0449 lpfc_restrict_login attribute cannot "
1375                                  "be set to %d, allowed range is [0, 1]\n",
1376                                  val);
1377                 vport->cfg_restrict_login = 1;
1378                 return -EINVAL;
1379         }
1380         if (vport->port_type == LPFC_PHYSICAL_PORT) {
1381                 vport->cfg_restrict_login = 0;
1382                 return 0;
1383         }
1384         vport->cfg_restrict_login = val;
1385         return 0;
1386 }
1387
1388 static int
1389 lpfc_restrict_login_set(struct lpfc_vport *vport, int val)
1390 {
1391         if (val < 0 || val > 1) {
1392                 lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
1393                                  "0450 lpfc_restrict_login attribute cannot "
1394                                  "be set to %d, allowed range is [0, 1]\n",
1395                                  val);
1396                 vport->cfg_restrict_login = 1;
1397                 return -EINVAL;
1398         }
1399         if (vport->port_type == LPFC_PHYSICAL_PORT && val != 0) {
1400                 lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
1401                                  "0468 lpfc_restrict_login must be 0 for "
1402                                  "Physical ports.\n");
1403                 vport->cfg_restrict_login = 0;
1404                 return 0;
1405         }
1406         vport->cfg_restrict_login = val;
1407         return 0;
1408 }
1409 lpfc_vport_param_store(restrict_login);
1410 static DEVICE_ATTR(lpfc_restrict_login, S_IRUGO | S_IWUSR,
1411                    lpfc_restrict_login_show, lpfc_restrict_login_store);
1412
1413 /*
1414 # Some disk devices have a "select ID" or "select Target" capability.
1415 # From a protocol standpoint "select ID" usually means select the
1416 # Fibre channel "ALPA".  In the FC-AL Profile there is an "informative
1417 # annex" which contains a table that maps a "select ID" (a number
1418 # between 0 and 7F) to an ALPA.  By default, for compatibility with
1419 # older drivers, the lpfc driver scans this table from low ALPA to high
1420 # ALPA.
1421 #
1422 # Turning on the scan-down variable (on  = 1, off = 0) will
1423 # cause the lpfc driver to use an inverted table, effectively
1424 # scanning ALPAs from high to low. Value range is [0,1]. Default value is 1.
1425 #
1426 # (Note: This "select ID" functionality is a LOOP ONLY characteristic
1427 # and will not work across a fabric. Also this parameter will take
1428 # effect only in the case when ALPA map is not available.)
1429 */
1430 LPFC_VPORT_ATTR_R(scan_down, 1, 0, 1,
1431                   "Start scanning for devices from highest ALPA to lowest");
1432
1433 /*
1434 # lpfc_topology:  link topology for init link
1435 #            0x0  = attempt loop mode then point-to-point
1436 #            0x01 = internal loopback mode
1437 #            0x02 = attempt point-to-point mode only
1438 #            0x04 = attempt loop mode only
1439 #            0x06 = attempt point-to-point mode then loop
1440 # Set point-to-point mode if you want to run as an N_Port.
1441 # Set loop mode if you want to run as an NL_Port. Value range is [0,0x6].
1442 # Default value is 0.
1443 */
1444 static int
1445 lpfc_topology_set(struct lpfc_hba *phba, int val)
1446 {
1447         int err;
1448         uint32_t prev_val;
1449         if (val >= 0 && val <= 6) {
1450                 prev_val = phba->cfg_topology;
1451                 phba->cfg_topology = val;
1452                 err = lpfc_issue_lip(lpfc_shost_from_vport(phba->pport));
1453                 if (err)
1454                         phba->cfg_topology = prev_val;
1455                 return err;
1456         }
1457         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1458                 "%d:0467 lpfc_topology attribute cannot be set to %d, "
1459                 "allowed range is [0, 6]\n",
1460                 phba->brd_no, val);
1461         return -EINVAL;
1462 }
1463 static int lpfc_topology = 0;
1464 module_param(lpfc_topology, int, 0);
1465 MODULE_PARM_DESC(lpfc_topology, "Select Fibre Channel topology");
1466 lpfc_param_show(topology)
1467 lpfc_param_init(topology, 0, 0, 6)
1468 lpfc_param_store(topology)
1469 static DEVICE_ATTR(lpfc_topology, S_IRUGO | S_IWUSR,
1470                 lpfc_topology_show, lpfc_topology_store);
1471
1472 /*
1473 # lpfc_link_speed: Link speed selection for initializing the Fibre Channel
1474 # connection.
1475 #       0  = auto select (default)
1476 #       1  = 1 Gigabaud
1477 #       2  = 2 Gigabaud
1478 #       4  = 4 Gigabaud
1479 #       8  = 8 Gigabaud
1480 # Value range is [0,8]. Default value is 0.
1481 */
1482 static int
1483 lpfc_link_speed_set(struct lpfc_hba *phba, int val)
1484 {
1485         int err;
1486         uint32_t prev_val;
1487
1488         if (((val == LINK_SPEED_1G) && !(phba->lmt & LMT_1Gb)) ||
1489                 ((val == LINK_SPEED_2G) && !(phba->lmt & LMT_2Gb)) ||
1490                 ((val == LINK_SPEED_4G) && !(phba->lmt & LMT_4Gb)) ||
1491                 ((val == LINK_SPEED_8G) && !(phba->lmt & LMT_8Gb)) ||
1492                 ((val == LINK_SPEED_10G) && !(phba->lmt & LMT_10Gb)))
1493                 return -EINVAL;
1494
1495         if ((val >= 0 && val <= LPFC_MAX_LINK_SPEED)
1496                 && (LPFC_LINK_SPEED_BITMAP & (1 << val))) {
1497                 prev_val = phba->cfg_link_speed;
1498                 phba->cfg_link_speed = val;
1499                 err = lpfc_issue_lip(lpfc_shost_from_vport(phba->pport));
1500                 if (err)
1501                         phba->cfg_link_speed = prev_val;
1502                 return err;
1503         }
1504
1505         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1506                 "%d:0469 lpfc_link_speed attribute cannot be set to %d, "
1507                 "allowed range is [0, 8]\n",
1508                 phba->brd_no, val);
1509         return -EINVAL;
1510 }
1511
1512 static int lpfc_link_speed = 0;
1513 module_param(lpfc_link_speed, int, 0);
1514 MODULE_PARM_DESC(lpfc_link_speed, "Select link speed");
1515 lpfc_param_show(link_speed)
1516 static int
1517 lpfc_link_speed_init(struct lpfc_hba *phba, int val)
1518 {
1519         if ((val >= 0 && val <= LPFC_MAX_LINK_SPEED)
1520                 && (LPFC_LINK_SPEED_BITMAP & (1 << val))) {
1521                 phba->cfg_link_speed = val;
1522                 return 0;
1523         }
1524         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1525                         "0454 lpfc_link_speed attribute cannot "
1526                         "be set to %d, allowed values are "
1527                         "["LPFC_LINK_SPEED_STRING"]\n", val);
1528         phba->cfg_link_speed = 0;
1529         return -EINVAL;
1530 }
1531
1532 lpfc_param_store(link_speed)
1533 static DEVICE_ATTR(lpfc_link_speed, S_IRUGO | S_IWUSR,
1534                 lpfc_link_speed_show, lpfc_link_speed_store);
1535
1536 /*
1537 # lpfc_fcp_class:  Determines FC class to use for the FCP protocol.
1538 # Value range is [2,3]. Default value is 3.
1539 */
1540 LPFC_VPORT_ATTR_R(fcp_class, 3, 2, 3,
1541                   "Select Fibre Channel class of service for FCP sequences");
1542
1543 /*
1544 # lpfc_use_adisc: Use ADISC for FCP rediscovery instead of PLOGI. Value range
1545 # is [0,1]. Default value is 0.
1546 */
1547 LPFC_VPORT_ATTR_RW(use_adisc, 0, 0, 1,
1548                    "Use ADISC on rediscovery to authenticate FCP devices");
1549
1550 /*
1551 # lpfc_ack0: Use ACK0, instead of ACK1 for class 2 acknowledgement. Value
1552 # range is [0,1]. Default value is 0.
1553 */
1554 LPFC_ATTR_R(ack0, 0, 0, 1, "Enable ACK0 support");
1555
1556 /*
1557 # lpfc_cr_delay & lpfc_cr_count: Default values for I/O colaesing
1558 # cr_delay (msec) or cr_count outstanding commands. cr_delay can take
1559 # value [0,63]. cr_count can take value [1,255]. Default value of cr_delay
1560 # is 0. Default value of cr_count is 1. The cr_count feature is disabled if
1561 # cr_delay is set to 0.
1562 */
1563 LPFC_ATTR_RW(cr_delay, 0, 0, 63, "A count of milliseconds after which an "
1564                 "interrupt response is generated");
1565
1566 LPFC_ATTR_RW(cr_count, 1, 1, 255, "A count of I/O completions after which an "
1567                 "interrupt response is generated");
1568
1569 /*
1570 # lpfc_multi_ring_support:  Determines how many rings to spread available
1571 # cmd/rsp IOCB entries across.
1572 # Value range is [1,2]. Default value is 1.
1573 */
1574 LPFC_ATTR_R(multi_ring_support, 1, 1, 2, "Determines number of primary "
1575                 "SLI rings to spread IOCB entries across");
1576
1577 /*
1578 # lpfc_multi_ring_rctl:  If lpfc_multi_ring_support is enabled, this
1579 # identifies what rctl value to configure the additional ring for.
1580 # Value range is [1,0xff]. Default value is 4 (Unsolicated Data).
1581 */
1582 LPFC_ATTR_R(multi_ring_rctl, FC_UNSOL_DATA, 1,
1583              255, "Identifies RCTL for additional ring configuration");
1584
1585 /*
1586 # lpfc_multi_ring_type:  If lpfc_multi_ring_support is enabled, this
1587 # identifies what type value to configure the additional ring for.
1588 # Value range is [1,0xff]. Default value is 5 (LLC/SNAP).
1589 */
1590 LPFC_ATTR_R(multi_ring_type, FC_LLC_SNAP, 1,
1591              255, "Identifies TYPE for additional ring configuration");
1592
1593 /*
1594 # lpfc_fdmi_on: controls FDMI support.
1595 #       0 = no FDMI support
1596 #       1 = support FDMI without attribute of hostname
1597 #       2 = support FDMI with attribute of hostname
1598 # Value range [0,2]. Default value is 0.
1599 */
1600 LPFC_VPORT_ATTR_RW(fdmi_on, 0, 0, 2, "Enable FDMI support");
1601
1602 /*
1603 # Specifies the maximum number of ELS cmds we can have outstanding (for
1604 # discovery). Value range is [1,64]. Default value = 32.
1605 */
1606 LPFC_VPORT_ATTR(discovery_threads, 32, 1, 64, "Maximum number of ELS commands "
1607                  "during discovery");
1608
1609 /*
1610 # lpfc_max_luns: maximum allowed LUN.
1611 # Value range is [0,65535]. Default value is 255.
1612 # NOTE: The SCSI layer might probe all allowed LUN on some old targets.
1613 */
1614 LPFC_VPORT_ATTR_R(max_luns, 255, 0, 65535, "Maximum allowed LUN");
1615
1616 /*
1617 # lpfc_poll_tmo: .Milliseconds driver will wait between polling FCP ring.
1618 # Value range is [1,255], default value is 10.
1619 */
1620 LPFC_ATTR_RW(poll_tmo, 10, 1, 255,
1621              "Milliseconds driver will wait between polling FCP ring");
1622
1623 /*
1624 # lpfc_use_msi: Use MSI (Message Signaled Interrupts) in systems that
1625 #               support this feature
1626 #       0  = MSI disabled (default)
1627 #       1  = MSI enabled
1628 #       2  = MSI-X enabled
1629 # Value range is [0,2]. Default value is 0.
1630 */
1631 LPFC_ATTR_R(use_msi, 0, 0, 2, "Use Message Signaled Interrupts (1) or "
1632             "MSI-X (2), if possible");
1633
1634 /*
1635 # lpfc_enable_hba_reset: Allow or prevent HBA resets to the hardware.
1636 #       0  = HBA resets disabled
1637 #       1  = HBA resets enabled (default)
1638 # Value range is [0,1]. Default value is 1.
1639 */
1640 LPFC_ATTR_R(enable_hba_reset, 1, 0, 1, "Enable HBA resets from the driver.");
1641
1642 /*
1643 # lpfc_enable_hba_heartbeat: Enable HBA heartbeat timer..
1644 #       0  = HBA Heartbeat disabled
1645 #       1  = HBA Heartbeat enabled (default)
1646 # Value range is [0,1]. Default value is 1.
1647 */
1648 LPFC_ATTR_R(enable_hba_heartbeat, 1, 0, 1, "Enable HBA Heartbeat.");
1649
1650 /*
1651  * lpfc_sg_seg_cnt: Initial Maximum DMA Segment Count
1652  * This value can be set to values between 64 and 256. The default value is
1653  * 64, but may be increased to allow for larger Max I/O sizes. The scsi layer
1654  * will be allowed to request I/Os of sizes up to (MAX_SEG_COUNT * SEG_SIZE).
1655  */
1656 LPFC_ATTR_R(sg_seg_cnt, LPFC_DEFAULT_SG_SEG_CNT, LPFC_DEFAULT_SG_SEG_CNT,
1657             LPFC_MAX_SG_SEG_CNT, "Max Scatter Gather Segment Count");
1658
1659 struct device_attribute *lpfc_hba_attrs[] = {
1660         &dev_attr_info,
1661         &dev_attr_serialnum,
1662         &dev_attr_modeldesc,
1663         &dev_attr_modelname,
1664         &dev_attr_programtype,
1665         &dev_attr_portnum,
1666         &dev_attr_fwrev,
1667         &dev_attr_hdw,
1668         &dev_attr_option_rom_version,
1669         &dev_attr_link_state,
1670         &dev_attr_num_discovered_ports,
1671         &dev_attr_lpfc_drvr_version,
1672         &dev_attr_lpfc_temp_sensor,
1673         &dev_attr_lpfc_log_verbose,
1674         &dev_attr_lpfc_lun_queue_depth,
1675         &dev_attr_lpfc_hba_queue_depth,
1676         &dev_attr_lpfc_peer_port_login,
1677         &dev_attr_lpfc_nodev_tmo,
1678         &dev_attr_lpfc_devloss_tmo,
1679         &dev_attr_lpfc_fcp_class,
1680         &dev_attr_lpfc_use_adisc,
1681         &dev_attr_lpfc_ack0,
1682         &dev_attr_lpfc_topology,
1683         &dev_attr_lpfc_scan_down,
1684         &dev_attr_lpfc_link_speed,
1685         &dev_attr_lpfc_cr_delay,
1686         &dev_attr_lpfc_cr_count,
1687         &dev_attr_lpfc_multi_ring_support,
1688         &dev_attr_lpfc_multi_ring_rctl,
1689         &dev_attr_lpfc_multi_ring_type,
1690         &dev_attr_lpfc_fdmi_on,
1691         &dev_attr_lpfc_max_luns,
1692         &dev_attr_lpfc_enable_npiv,
1693         &dev_attr_nport_evt_cnt,
1694         &dev_attr_board_mode,
1695         &dev_attr_max_vpi,
1696         &dev_attr_used_vpi,
1697         &dev_attr_max_rpi,
1698         &dev_attr_used_rpi,
1699         &dev_attr_max_xri,
1700         &dev_attr_used_xri,
1701         &dev_attr_npiv_info,
1702         &dev_attr_issue_reset,
1703         &dev_attr_lpfc_poll,
1704         &dev_attr_lpfc_poll_tmo,
1705         &dev_attr_lpfc_use_msi,
1706         &dev_attr_lpfc_soft_wwnn,
1707         &dev_attr_lpfc_soft_wwpn,
1708         &dev_attr_lpfc_soft_wwn_enable,
1709         &dev_attr_lpfc_enable_hba_reset,
1710         &dev_attr_lpfc_enable_hba_heartbeat,
1711         &dev_attr_lpfc_sg_seg_cnt,
1712         NULL,
1713 };
1714
1715 struct device_attribute *lpfc_vport_attrs[] = {
1716         &dev_attr_info,
1717         &dev_attr_link_state,
1718         &dev_attr_num_discovered_ports,
1719         &dev_attr_lpfc_drvr_version,
1720         &dev_attr_lpfc_log_verbose,
1721         &dev_attr_lpfc_lun_queue_depth,
1722         &dev_attr_lpfc_nodev_tmo,
1723         &dev_attr_lpfc_devloss_tmo,
1724         &dev_attr_lpfc_hba_queue_depth,
1725         &dev_attr_lpfc_peer_port_login,
1726         &dev_attr_lpfc_restrict_login,
1727         &dev_attr_lpfc_fcp_class,
1728         &dev_attr_lpfc_use_adisc,
1729         &dev_attr_lpfc_fdmi_on,
1730         &dev_attr_lpfc_max_luns,
1731         &dev_attr_nport_evt_cnt,
1732         &dev_attr_npiv_info,
1733         &dev_attr_lpfc_enable_da_id,
1734         NULL,
1735 };
1736
1737 static ssize_t
1738 sysfs_ctlreg_write(struct kobject *kobj, struct bin_attribute *bin_attr,
1739                    char *buf, loff_t off, size_t count)
1740 {
1741         size_t buf_off;
1742         struct device *dev = container_of(kobj, struct device, kobj);
1743         struct Scsi_Host  *shost = class_to_shost(dev);
1744         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1745         struct lpfc_hba   *phba = vport->phba;
1746
1747         if ((off + count) > FF_REG_AREA_SIZE)
1748                 return -ERANGE;
1749
1750         if (count == 0) return 0;
1751
1752         if (off % 4 || count % 4 || (unsigned long)buf % 4)
1753                 return -EINVAL;
1754
1755         if (!(vport->fc_flag & FC_OFFLINE_MODE)) {
1756                 return -EPERM;
1757         }
1758
1759         spin_lock_irq(&phba->hbalock);
1760         for (buf_off = 0; buf_off < count; buf_off += sizeof(uint32_t))
1761                 writel(*((uint32_t *)(buf + buf_off)),
1762                        phba->ctrl_regs_memmap_p + off + buf_off);
1763
1764         spin_unlock_irq(&phba->hbalock);
1765
1766         return count;
1767 }
1768
1769 static ssize_t
1770 sysfs_ctlreg_read(struct kobject *kobj, struct bin_attribute *bin_attr,
1771                   char *buf, loff_t off, size_t count)
1772 {
1773         size_t buf_off;
1774         uint32_t * tmp_ptr;
1775         struct device *dev = container_of(kobj, struct device, kobj);
1776         struct Scsi_Host  *shost = class_to_shost(dev);
1777         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1778         struct lpfc_hba   *phba = vport->phba;
1779
1780         if (off > FF_REG_AREA_SIZE)
1781                 return -ERANGE;
1782
1783         if ((off + count) > FF_REG_AREA_SIZE)
1784                 count = FF_REG_AREA_SIZE - off;
1785
1786         if (count == 0) return 0;
1787
1788         if (off % 4 || count % 4 || (unsigned long)buf % 4)
1789                 return -EINVAL;
1790
1791         spin_lock_irq(&phba->hbalock);
1792
1793         for (buf_off = 0; buf_off < count; buf_off += sizeof(uint32_t)) {
1794                 tmp_ptr = (uint32_t *)(buf + buf_off);
1795                 *tmp_ptr = readl(phba->ctrl_regs_memmap_p + off + buf_off);
1796         }
1797
1798         spin_unlock_irq(&phba->hbalock);
1799
1800         return count;
1801 }
1802
1803 static struct bin_attribute sysfs_ctlreg_attr = {
1804         .attr = {
1805                 .name = "ctlreg",
1806                 .mode = S_IRUSR | S_IWUSR,
1807         },
1808         .size = 256,
1809         .read = sysfs_ctlreg_read,
1810         .write = sysfs_ctlreg_write,
1811 };
1812
1813
1814 static void
1815 sysfs_mbox_idle(struct lpfc_hba *phba)
1816 {
1817         phba->sysfs_mbox.state = SMBOX_IDLE;
1818         phba->sysfs_mbox.offset = 0;
1819
1820         if (phba->sysfs_mbox.mbox) {
1821                 mempool_free(phba->sysfs_mbox.mbox,
1822                              phba->mbox_mem_pool);
1823                 phba->sysfs_mbox.mbox = NULL;
1824         }
1825 }
1826
1827 static ssize_t
1828 sysfs_mbox_write(struct kobject *kobj, struct bin_attribute *bin_attr,
1829                  char *buf, loff_t off, size_t count)
1830 {
1831         struct device *dev = container_of(kobj, struct device, kobj);
1832         struct Scsi_Host  *shost = class_to_shost(dev);
1833         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1834         struct lpfc_hba   *phba = vport->phba;
1835         struct lpfcMboxq  *mbox = NULL;
1836
1837         if ((count + off) > MAILBOX_CMD_SIZE)
1838                 return -ERANGE;
1839
1840         if (off % 4 ||  count % 4 || (unsigned long)buf % 4)
1841                 return -EINVAL;
1842
1843         if (count == 0)
1844                 return 0;
1845
1846         if (off == 0) {
1847                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1848                 if (!mbox)
1849                         return -ENOMEM;
1850                 memset(mbox, 0, sizeof (LPFC_MBOXQ_t));
1851         }
1852
1853         spin_lock_irq(&phba->hbalock);
1854
1855         if (off == 0) {
1856                 if (phba->sysfs_mbox.mbox)
1857                         mempool_free(mbox, phba->mbox_mem_pool);
1858                 else
1859                         phba->sysfs_mbox.mbox = mbox;
1860                 phba->sysfs_mbox.state = SMBOX_WRITING;
1861         } else {
1862                 if (phba->sysfs_mbox.state  != SMBOX_WRITING ||
1863                     phba->sysfs_mbox.offset != off           ||
1864                     phba->sysfs_mbox.mbox   == NULL) {
1865                         sysfs_mbox_idle(phba);
1866                         spin_unlock_irq(&phba->hbalock);
1867                         return -EAGAIN;
1868                 }
1869         }
1870
1871         memcpy((uint8_t *) & phba->sysfs_mbox.mbox->mb + off,
1872                buf, count);
1873
1874         phba->sysfs_mbox.offset = off + count;
1875
1876         spin_unlock_irq(&phba->hbalock);
1877
1878         return count;
1879 }
1880
1881 static ssize_t
1882 sysfs_mbox_read(struct kobject *kobj, struct bin_attribute *bin_attr,
1883                 char *buf, loff_t off, size_t count)
1884 {
1885         struct device *dev = container_of(kobj, struct device, kobj);
1886         struct Scsi_Host  *shost = class_to_shost(dev);
1887         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1888         struct lpfc_hba   *phba = vport->phba;
1889         int rc;
1890
1891         if (off > MAILBOX_CMD_SIZE)
1892                 return -ERANGE;
1893
1894         if ((count + off) > MAILBOX_CMD_SIZE)
1895                 count = MAILBOX_CMD_SIZE - off;
1896
1897         if (off % 4 ||  count % 4 || (unsigned long)buf % 4)
1898                 return -EINVAL;
1899
1900         if (off && count == 0)
1901                 return 0;
1902
1903         spin_lock_irq(&phba->hbalock);
1904
1905         if (phba->over_temp_state == HBA_OVER_TEMP) {
1906                 sysfs_mbox_idle(phba);
1907                 spin_unlock_irq(&phba->hbalock);
1908                 return  -EACCES;
1909         }
1910
1911         if (off == 0 &&
1912             phba->sysfs_mbox.state  == SMBOX_WRITING &&
1913             phba->sysfs_mbox.offset >= 2 * sizeof(uint32_t)) {
1914
1915                 switch (phba->sysfs_mbox.mbox->mb.mbxCommand) {
1916                         /* Offline only */
1917                 case MBX_INIT_LINK:
1918                 case MBX_DOWN_LINK:
1919                 case MBX_CONFIG_LINK:
1920                 case MBX_CONFIG_RING:
1921                 case MBX_RESET_RING:
1922                 case MBX_UNREG_LOGIN:
1923                 case MBX_CLEAR_LA:
1924                 case MBX_DUMP_CONTEXT:
1925                 case MBX_RUN_DIAGS:
1926                 case MBX_RESTART:
1927                 case MBX_SET_MASK:
1928                 case MBX_SET_DEBUG:
1929                         if (!(vport->fc_flag & FC_OFFLINE_MODE)) {
1930                                 printk(KERN_WARNING "mbox_read:Command 0x%x "
1931                                        "is illegal in on-line state\n",
1932                                        phba->sysfs_mbox.mbox->mb.mbxCommand);
1933                                 sysfs_mbox_idle(phba);
1934                                 spin_unlock_irq(&phba->hbalock);
1935                                 return -EPERM;
1936                         }
1937                 case MBX_WRITE_NV:
1938                 case MBX_WRITE_VPARMS:
1939                 case MBX_LOAD_SM:
1940                 case MBX_READ_NV:
1941                 case MBX_READ_CONFIG:
1942                 case MBX_READ_RCONFIG:
1943                 case MBX_READ_STATUS:
1944                 case MBX_READ_XRI:
1945                 case MBX_READ_REV:
1946                 case MBX_READ_LNK_STAT:
1947                 case MBX_DUMP_MEMORY:
1948                 case MBX_DOWN_LOAD:
1949                 case MBX_UPDATE_CFG:
1950                 case MBX_KILL_BOARD:
1951                 case MBX_LOAD_AREA:
1952                 case MBX_LOAD_EXP_ROM:
1953                 case MBX_BEACON:
1954                 case MBX_DEL_LD_ENTRY:
1955                 case MBX_SET_VARIABLE:
1956                 case MBX_WRITE_WWN:
1957                         break;
1958                 case MBX_READ_SPARM64:
1959                 case MBX_READ_LA:
1960                 case MBX_READ_LA64:
1961                 case MBX_REG_LOGIN:
1962                 case MBX_REG_LOGIN64:
1963                 case MBX_CONFIG_PORT:
1964                 case MBX_RUN_BIU_DIAG:
1965                         printk(KERN_WARNING "mbox_read: Illegal Command 0x%x\n",
1966                                phba->sysfs_mbox.mbox->mb.mbxCommand);
1967                         sysfs_mbox_idle(phba);
1968                         spin_unlock_irq(&phba->hbalock);
1969                         return -EPERM;
1970                 default:
1971                         printk(KERN_WARNING "mbox_read: Unknown Command 0x%x\n",
1972                                phba->sysfs_mbox.mbox->mb.mbxCommand);
1973                         sysfs_mbox_idle(phba);
1974                         spin_unlock_irq(&phba->hbalock);
1975                         return -EPERM;
1976                 }
1977
1978                 /* If HBA encountered an error attention, allow only DUMP
1979                  * or RESTART mailbox commands until the HBA is restarted.
1980                  */
1981                 if ((phba->pport->stopped) &&
1982                         (phba->sysfs_mbox.mbox->mb.mbxCommand !=
1983                                 MBX_DUMP_MEMORY &&
1984                          phba->sysfs_mbox.mbox->mb.mbxCommand !=
1985                                 MBX_RESTART &&
1986                          phba->sysfs_mbox.mbox->mb.mbxCommand !=
1987                                 MBX_WRITE_VPARMS)) {
1988                         sysfs_mbox_idle(phba);
1989                         spin_unlock_irq(&phba->hbalock);
1990                         return -EPERM;
1991                 }
1992
1993                 phba->sysfs_mbox.mbox->vport = vport;
1994
1995                 /* Don't allow mailbox commands to be sent when blocked
1996                  * or when in the middle of discovery
1997                  */
1998                 if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
1999                         sysfs_mbox_idle(phba);
2000                         spin_unlock_irq(&phba->hbalock);
2001                         return  -EAGAIN;
2002                 }
2003
2004                 if ((vport->fc_flag & FC_OFFLINE_MODE) ||
2005                     (!(phba->sli.sli_flag & LPFC_SLI2_ACTIVE))){
2006
2007                         spin_unlock_irq(&phba->hbalock);
2008                         rc = lpfc_sli_issue_mbox (phba,
2009                                                   phba->sysfs_mbox.mbox,
2010                                                   MBX_POLL);
2011                         spin_lock_irq(&phba->hbalock);
2012
2013                 } else {
2014                         spin_unlock_irq(&phba->hbalock);
2015                         rc = lpfc_sli_issue_mbox_wait (phba,
2016                                                        phba->sysfs_mbox.mbox,
2017                                 lpfc_mbox_tmo_val(phba,
2018                                     phba->sysfs_mbox.mbox->mb.mbxCommand) * HZ);
2019                         spin_lock_irq(&phba->hbalock);
2020                 }
2021
2022                 if (rc != MBX_SUCCESS) {
2023                         if (rc == MBX_TIMEOUT) {
2024                                 phba->sysfs_mbox.mbox = NULL;
2025                         }
2026                         sysfs_mbox_idle(phba);
2027                         spin_unlock_irq(&phba->hbalock);
2028                         return  (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
2029                 }
2030                 phba->sysfs_mbox.state = SMBOX_READING;
2031         }
2032         else if (phba->sysfs_mbox.offset != off ||
2033                  phba->sysfs_mbox.state  != SMBOX_READING) {
2034                 printk(KERN_WARNING  "mbox_read: Bad State\n");
2035                 sysfs_mbox_idle(phba);
2036                 spin_unlock_irq(&phba->hbalock);
2037                 return -EAGAIN;
2038         }
2039
2040         memcpy(buf, (uint8_t *) & phba->sysfs_mbox.mbox->mb + off, count);
2041
2042         phba->sysfs_mbox.offset = off + count;
2043
2044         if (phba->sysfs_mbox.offset == MAILBOX_CMD_SIZE)
2045                 sysfs_mbox_idle(phba);
2046
2047         spin_unlock_irq(&phba->hbalock);
2048
2049         return count;
2050 }
2051
2052 static struct bin_attribute sysfs_mbox_attr = {
2053         .attr = {
2054                 .name = "mbox",
2055                 .mode = S_IRUSR | S_IWUSR,
2056         },
2057         .size = MAILBOX_CMD_SIZE,
2058         .read = sysfs_mbox_read,
2059         .write = sysfs_mbox_write,
2060 };
2061
2062 int
2063 lpfc_alloc_sysfs_attr(struct lpfc_vport *vport)
2064 {
2065         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2066         int error;
2067
2068         error = sysfs_create_bin_file(&shost->shost_dev.kobj,
2069                                       &sysfs_ctlreg_attr);
2070         if (error)
2071                 goto out;
2072
2073         error = sysfs_create_bin_file(&shost->shost_dev.kobj,
2074                                       &sysfs_mbox_attr);
2075         if (error)
2076                 goto out_remove_ctlreg_attr;
2077
2078         return 0;
2079 out_remove_ctlreg_attr:
2080         sysfs_remove_bin_file(&shost->shost_dev.kobj, &sysfs_ctlreg_attr);
2081 out:
2082         return error;
2083 }
2084
2085 void
2086 lpfc_free_sysfs_attr(struct lpfc_vport *vport)
2087 {
2088         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2089
2090         sysfs_remove_bin_file(&shost->shost_dev.kobj, &sysfs_mbox_attr);
2091         sysfs_remove_bin_file(&shost->shost_dev.kobj, &sysfs_ctlreg_attr);
2092 }
2093
2094
2095 /*
2096  * Dynamic FC Host Attributes Support
2097  */
2098
2099 static void
2100 lpfc_get_host_port_id(struct Scsi_Host *shost)
2101 {
2102         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2103
2104         /* note: fc_myDID already in cpu endianness */
2105         fc_host_port_id(shost) = vport->fc_myDID;
2106 }
2107
2108 static void
2109 lpfc_get_host_port_type(struct Scsi_Host *shost)
2110 {
2111         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2112         struct lpfc_hba   *phba = vport->phba;
2113
2114         spin_lock_irq(shost->host_lock);
2115
2116         if (vport->port_type == LPFC_NPIV_PORT) {
2117                 fc_host_port_type(shost) = FC_PORTTYPE_NPIV;
2118         } else if (lpfc_is_link_up(phba)) {
2119                 if (phba->fc_topology == TOPOLOGY_LOOP) {
2120                         if (vport->fc_flag & FC_PUBLIC_LOOP)
2121                                 fc_host_port_type(shost) = FC_PORTTYPE_NLPORT;
2122                         else
2123                                 fc_host_port_type(shost) = FC_PORTTYPE_LPORT;
2124                 } else {
2125                         if (vport->fc_flag & FC_FABRIC)
2126                                 fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
2127                         else
2128                                 fc_host_port_type(shost) = FC_PORTTYPE_PTP;
2129                 }
2130         } else
2131                 fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
2132
2133         spin_unlock_irq(shost->host_lock);
2134 }
2135
2136 static void
2137 lpfc_get_host_port_state(struct Scsi_Host *shost)
2138 {
2139         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2140         struct lpfc_hba   *phba = vport->phba;
2141
2142         spin_lock_irq(shost->host_lock);
2143
2144         if (vport->fc_flag & FC_OFFLINE_MODE)
2145                 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
2146         else {
2147                 switch (phba->link_state) {
2148                 case LPFC_LINK_UNKNOWN:
2149                 case LPFC_LINK_DOWN:
2150                         fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
2151                         break;
2152                 case LPFC_LINK_UP:
2153                 case LPFC_CLEAR_LA:
2154                 case LPFC_HBA_READY:
2155                         /* Links up, beyond this port_type reports state */
2156                         fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
2157                         break;
2158                 case LPFC_HBA_ERROR:
2159                         fc_host_port_state(shost) = FC_PORTSTATE_ERROR;
2160                         break;
2161                 default:
2162                         fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
2163                         break;
2164                 }
2165         }
2166
2167         spin_unlock_irq(shost->host_lock);
2168 }
2169
2170 static void
2171 lpfc_get_host_speed(struct Scsi_Host *shost)
2172 {
2173         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2174         struct lpfc_hba   *phba = vport->phba;
2175
2176         spin_lock_irq(shost->host_lock);
2177
2178         if (lpfc_is_link_up(phba)) {
2179                 switch(phba->fc_linkspeed) {
2180                         case LA_1GHZ_LINK:
2181                                 fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
2182                         break;
2183                         case LA_2GHZ_LINK:
2184                                 fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
2185                         break;
2186                         case LA_4GHZ_LINK:
2187                                 fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
2188                         break;
2189                         case LA_8GHZ_LINK:
2190                                 fc_host_speed(shost) = FC_PORTSPEED_8GBIT;
2191                         break;
2192                         default:
2193                                 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
2194                         break;
2195                 }
2196         } else
2197                 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
2198
2199         spin_unlock_irq(shost->host_lock);
2200 }
2201
2202 static void
2203 lpfc_get_host_fabric_name (struct Scsi_Host *shost)
2204 {
2205         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2206         struct lpfc_hba   *phba = vport->phba;
2207         u64 node_name;
2208
2209         spin_lock_irq(shost->host_lock);
2210
2211         if ((vport->fc_flag & FC_FABRIC) ||
2212             ((phba->fc_topology == TOPOLOGY_LOOP) &&
2213              (vport->fc_flag & FC_PUBLIC_LOOP)))
2214                 node_name = wwn_to_u64(phba->fc_fabparam.nodeName.u.wwn);
2215         else
2216                 /* fabric is local port if there is no F/FL_Port */
2217                 node_name = 0;
2218
2219         spin_unlock_irq(shost->host_lock);
2220
2221         fc_host_fabric_name(shost) = node_name;
2222 }
2223
2224 static struct fc_host_statistics *
2225 lpfc_get_stats(struct Scsi_Host *shost)
2226 {
2227         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2228         struct lpfc_hba   *phba = vport->phba;
2229         struct lpfc_sli   *psli = &phba->sli;
2230         struct fc_host_statistics *hs = &phba->link_stats;
2231         struct lpfc_lnk_stat * lso = &psli->lnk_stat_offsets;
2232         LPFC_MBOXQ_t *pmboxq;
2233         MAILBOX_t *pmb;
2234         unsigned long seconds;
2235         int rc = 0;
2236
2237         /*
2238          * prevent udev from issuing mailbox commands until the port is
2239          * configured.
2240          */
2241         if (phba->link_state < LPFC_LINK_DOWN ||
2242             !phba->mbox_mem_pool ||
2243             (phba->sli.sli_flag & LPFC_SLI2_ACTIVE) == 0)
2244                 return NULL;
2245
2246         if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO)
2247                 return NULL;
2248
2249         pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2250         if (!pmboxq)
2251                 return NULL;
2252         memset(pmboxq, 0, sizeof (LPFC_MBOXQ_t));
2253
2254         pmb = &pmboxq->mb;
2255         pmb->mbxCommand = MBX_READ_STATUS;
2256         pmb->mbxOwner = OWN_HOST;
2257         pmboxq->context1 = NULL;
2258         pmboxq->vport = vport;
2259
2260         if ((vport->fc_flag & FC_OFFLINE_MODE) ||
2261                 (!(psli->sli_flag & LPFC_SLI2_ACTIVE)))
2262                 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
2263         else
2264                 rc = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
2265
2266         if (rc != MBX_SUCCESS) {
2267                 if (rc != MBX_TIMEOUT)
2268                         mempool_free(pmboxq, phba->mbox_mem_pool);
2269                 return NULL;
2270         }
2271
2272         memset(hs, 0, sizeof (struct fc_host_statistics));
2273
2274         hs->tx_frames = pmb->un.varRdStatus.xmitFrameCnt;
2275         hs->tx_words = (pmb->un.varRdStatus.xmitByteCnt * 256);
2276         hs->rx_frames = pmb->un.varRdStatus.rcvFrameCnt;
2277         hs->rx_words = (pmb->un.varRdStatus.rcvByteCnt * 256);
2278
2279         memset(pmboxq, 0, sizeof (LPFC_MBOXQ_t));
2280         pmb->mbxCommand = MBX_READ_LNK_STAT;
2281         pmb->mbxOwner = OWN_HOST;
2282         pmboxq->context1 = NULL;
2283         pmboxq->vport = vport;
2284
2285         if ((vport->fc_flag & FC_OFFLINE_MODE) ||
2286             (!(psli->sli_flag & LPFC_SLI2_ACTIVE)))
2287                 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
2288         else
2289                 rc = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
2290
2291         if (rc != MBX_SUCCESS) {
2292                 if (rc != MBX_TIMEOUT)
2293                         mempool_free(pmboxq, phba->mbox_mem_pool);
2294                 return NULL;
2295         }
2296
2297         hs->link_failure_count = pmb->un.varRdLnk.linkFailureCnt;
2298         hs->loss_of_sync_count = pmb->un.varRdLnk.lossSyncCnt;
2299         hs->loss_of_signal_count = pmb->un.varRdLnk.lossSignalCnt;
2300         hs->prim_seq_protocol_err_count = pmb->un.varRdLnk.primSeqErrCnt;
2301         hs->invalid_tx_word_count = pmb->un.varRdLnk.invalidXmitWord;
2302         hs->invalid_crc_count = pmb->un.varRdLnk.crcCnt;
2303         hs->error_frames = pmb->un.varRdLnk.crcCnt;
2304
2305         hs->link_failure_count -= lso->link_failure_count;
2306         hs->loss_of_sync_count -= lso->loss_of_sync_count;
2307         hs->loss_of_signal_count -= lso->loss_of_signal_count;
2308         hs->prim_seq_protocol_err_count -= lso->prim_seq_protocol_err_count;
2309         hs->invalid_tx_word_count -= lso->invalid_tx_word_count;
2310         hs->invalid_crc_count -= lso->invalid_crc_count;
2311         hs->error_frames -= lso->error_frames;
2312
2313         if (phba->fc_topology == TOPOLOGY_LOOP) {
2314                 hs->lip_count = (phba->fc_eventTag >> 1);
2315                 hs->lip_count -= lso->link_events;
2316                 hs->nos_count = -1;
2317         } else {
2318                 hs->lip_count = -1;
2319                 hs->nos_count = (phba->fc_eventTag >> 1);
2320                 hs->nos_count -= lso->link_events;
2321         }
2322
2323         hs->dumped_frames = -1;
2324
2325         seconds = get_seconds();
2326         if (seconds < psli->stats_start)
2327                 hs->seconds_since_last_reset = seconds +
2328                                 ((unsigned long)-1 - psli->stats_start);
2329         else
2330                 hs->seconds_since_last_reset = seconds - psli->stats_start;
2331
2332         mempool_free(pmboxq, phba->mbox_mem_pool);
2333
2334         return hs;
2335 }
2336
2337 static void
2338 lpfc_reset_stats(struct Scsi_Host *shost)
2339 {
2340         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2341         struct lpfc_hba   *phba = vport->phba;
2342         struct lpfc_sli   *psli = &phba->sli;
2343         struct lpfc_lnk_stat *lso = &psli->lnk_stat_offsets;
2344         LPFC_MBOXQ_t *pmboxq;
2345         MAILBOX_t *pmb;
2346         int rc = 0;
2347
2348         if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO)
2349                 return;
2350
2351         pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2352         if (!pmboxq)
2353                 return;
2354         memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
2355
2356         pmb = &pmboxq->mb;
2357         pmb->mbxCommand = MBX_READ_STATUS;
2358         pmb->mbxOwner = OWN_HOST;
2359         pmb->un.varWords[0] = 0x1; /* reset request */
2360         pmboxq->context1 = NULL;
2361         pmboxq->vport = vport;
2362
2363         if ((vport->fc_flag & FC_OFFLINE_MODE) ||
2364                 (!(psli->sli_flag & LPFC_SLI2_ACTIVE)))
2365                 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
2366         else
2367                 rc = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
2368
2369         if (rc != MBX_SUCCESS) {
2370                 if (rc != MBX_TIMEOUT)
2371                         mempool_free(pmboxq, phba->mbox_mem_pool);
2372                 return;
2373         }
2374
2375         memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
2376         pmb->mbxCommand = MBX_READ_LNK_STAT;
2377         pmb->mbxOwner = OWN_HOST;
2378         pmboxq->context1 = NULL;
2379         pmboxq->vport = vport;
2380
2381         if ((vport->fc_flag & FC_OFFLINE_MODE) ||
2382             (!(psli->sli_flag & LPFC_SLI2_ACTIVE)))
2383                 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
2384         else
2385                 rc = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
2386
2387         if (rc != MBX_SUCCESS) {
2388                 if (rc != MBX_TIMEOUT)
2389                         mempool_free( pmboxq, phba->mbox_mem_pool);
2390                 return;
2391         }
2392
2393         lso->link_failure_count = pmb->un.varRdLnk.linkFailureCnt;
2394         lso->loss_of_sync_count = pmb->un.varRdLnk.lossSyncCnt;
2395         lso->loss_of_signal_count = pmb->un.varRdLnk.lossSignalCnt;
2396         lso->prim_seq_protocol_err_count = pmb->un.varRdLnk.primSeqErrCnt;
2397         lso->invalid_tx_word_count = pmb->un.varRdLnk.invalidXmitWord;
2398         lso->invalid_crc_count = pmb->un.varRdLnk.crcCnt;
2399         lso->error_frames = pmb->un.varRdLnk.crcCnt;
2400         lso->link_events = (phba->fc_eventTag >> 1);
2401
2402         psli->stats_start = get_seconds();
2403
2404         mempool_free(pmboxq, phba->mbox_mem_pool);
2405
2406         return;
2407 }
2408
2409 /*
2410  * The LPFC driver treats linkdown handling as target loss events so there
2411  * are no sysfs handlers for link_down_tmo.
2412  */
2413
2414 static struct lpfc_nodelist *
2415 lpfc_get_node_by_target(struct scsi_target *starget)
2416 {
2417         struct Scsi_Host  *shost = dev_to_shost(starget->dev.parent);
2418         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2419         struct lpfc_nodelist *ndlp;
2420
2421         spin_lock_irq(shost->host_lock);
2422         /* Search for this, mapped, target ID */
2423         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
2424                 if (NLP_CHK_NODE_ACT(ndlp) &&
2425                     ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
2426                     starget->id == ndlp->nlp_sid) {
2427                         spin_unlock_irq(shost->host_lock);
2428                         return ndlp;
2429                 }
2430         }
2431         spin_unlock_irq(shost->host_lock);
2432         return NULL;
2433 }
2434
2435 static void
2436 lpfc_get_starget_port_id(struct scsi_target *starget)
2437 {
2438         struct lpfc_nodelist *ndlp = lpfc_get_node_by_target(starget);
2439
2440         fc_starget_port_id(starget) = ndlp ? ndlp->nlp_DID : -1;
2441 }
2442
2443 static void
2444 lpfc_get_starget_node_name(struct scsi_target *starget)
2445 {
2446         struct lpfc_nodelist *ndlp = lpfc_get_node_by_target(starget);
2447
2448         fc_starget_node_name(starget) =
2449                 ndlp ? wwn_to_u64(ndlp->nlp_nodename.u.wwn) : 0;
2450 }
2451
2452 static void
2453 lpfc_get_starget_port_name(struct scsi_target *starget)
2454 {
2455         struct lpfc_nodelist *ndlp = lpfc_get_node_by_target(starget);
2456
2457         fc_starget_port_name(starget) =
2458                 ndlp ? wwn_to_u64(ndlp->nlp_portname.u.wwn) : 0;
2459 }
2460
2461 static void
2462 lpfc_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
2463 {
2464         if (timeout)
2465                 rport->dev_loss_tmo = timeout;
2466         else
2467                 rport->dev_loss_tmo = 1;
2468 }
2469
2470
2471 #define lpfc_rport_show_function(field, format_string, sz, cast)        \
2472 static ssize_t                                                          \
2473 lpfc_show_rport_##field (struct device *dev,                            \
2474                          struct device_attribute *attr,                 \
2475                          char *buf)                                     \
2476 {                                                                       \
2477         struct fc_rport *rport = transport_class_to_rport(dev);         \
2478         struct lpfc_rport_data *rdata = rport->hostdata;                \
2479         return snprintf(buf, sz, format_string,                         \
2480                 (rdata->target) ? cast rdata->target->field : 0);       \
2481 }
2482
2483 #define lpfc_rport_rd_attr(field, format_string, sz)                    \
2484         lpfc_rport_show_function(field, format_string, sz, )            \
2485 static FC_RPORT_ATTR(field, S_IRUGO, lpfc_show_rport_##field, NULL)
2486
2487
2488 struct fc_function_template lpfc_transport_functions = {
2489         /* fixed attributes the driver supports */
2490         .show_host_node_name = 1,
2491         .show_host_port_name = 1,
2492         .show_host_supported_classes = 1,
2493         .show_host_supported_fc4s = 1,
2494         .show_host_supported_speeds = 1,
2495         .show_host_maxframe_size = 1,
2496
2497         /* dynamic attributes the driver supports */
2498         .get_host_port_id = lpfc_get_host_port_id,
2499         .show_host_port_id = 1,
2500
2501         .get_host_port_type = lpfc_get_host_port_type,
2502         .show_host_port_type = 1,
2503
2504         .get_host_port_state = lpfc_get_host_port_state,
2505         .show_host_port_state = 1,
2506
2507         /* active_fc4s is shown but doesn't change (thus no get function) */
2508         .show_host_active_fc4s = 1,
2509
2510         .get_host_speed = lpfc_get_host_speed,
2511         .show_host_speed = 1,
2512
2513         .get_host_fabric_name = lpfc_get_host_fabric_name,
2514         .show_host_fabric_name = 1,
2515
2516         /*
2517          * The LPFC driver treats linkdown handling as target loss events
2518          * so there are no sysfs handlers for link_down_tmo.
2519          */
2520
2521         .get_fc_host_stats = lpfc_get_stats,
2522         .reset_fc_host_stats = lpfc_reset_stats,
2523
2524         .dd_fcrport_size = sizeof(struct lpfc_rport_data),
2525         .show_rport_maxframe_size = 1,
2526         .show_rport_supported_classes = 1,
2527
2528         .set_rport_dev_loss_tmo = lpfc_set_rport_loss_tmo,
2529         .show_rport_dev_loss_tmo = 1,
2530
2531         .get_starget_port_id  = lpfc_get_starget_port_id,
2532         .show_starget_port_id = 1,
2533
2534         .get_starget_node_name = lpfc_get_starget_node_name,
2535         .show_starget_node_name = 1,
2536
2537         .get_starget_port_name = lpfc_get_starget_port_name,
2538         .show_starget_port_name = 1,
2539
2540         .issue_fc_host_lip = lpfc_issue_lip,
2541         .dev_loss_tmo_callbk = lpfc_dev_loss_tmo_callbk,
2542         .terminate_rport_io = lpfc_terminate_rport_io,
2543
2544         .dd_fcvport_size = sizeof(struct lpfc_vport *),
2545 };
2546
2547 struct fc_function_template lpfc_vport_transport_functions = {
2548         /* fixed attributes the driver supports */
2549         .show_host_node_name = 1,
2550         .show_host_port_name = 1,
2551         .show_host_supported_classes = 1,
2552         .show_host_supported_fc4s = 1,
2553         .show_host_supported_speeds = 1,
2554         .show_host_maxframe_size = 1,
2555
2556         /* dynamic attributes the driver supports */
2557         .get_host_port_id = lpfc_get_host_port_id,
2558         .show_host_port_id = 1,
2559
2560         .get_host_port_type = lpfc_get_host_port_type,
2561         .show_host_port_type = 1,
2562
2563         .get_host_port_state = lpfc_get_host_port_state,
2564         .show_host_port_state = 1,
2565
2566         /* active_fc4s is shown but doesn't change (thus no get function) */
2567         .show_host_active_fc4s = 1,
2568
2569         .get_host_speed = lpfc_get_host_speed,
2570         .show_host_speed = 1,
2571
2572         .get_host_fabric_name = lpfc_get_host_fabric_name,
2573         .show_host_fabric_name = 1,
2574
2575         /*
2576          * The LPFC driver treats linkdown handling as target loss events
2577          * so there are no sysfs handlers for link_down_tmo.
2578          */
2579
2580         .get_fc_host_stats = lpfc_get_stats,
2581         .reset_fc_host_stats = lpfc_reset_stats,
2582
2583         .dd_fcrport_size = sizeof(struct lpfc_rport_data),
2584         .show_rport_maxframe_size = 1,
2585         .show_rport_supported_classes = 1,
2586
2587         .set_rport_dev_loss_tmo = lpfc_set_rport_loss_tmo,
2588         .show_rport_dev_loss_tmo = 1,
2589
2590         .get_starget_port_id  = lpfc_get_starget_port_id,
2591         .show_starget_port_id = 1,
2592
2593         .get_starget_node_name = lpfc_get_starget_node_name,
2594         .show_starget_node_name = 1,
2595
2596         .get_starget_port_name = lpfc_get_starget_port_name,
2597         .show_starget_port_name = 1,
2598
2599         .dev_loss_tmo_callbk = lpfc_dev_loss_tmo_callbk,
2600         .terminate_rport_io = lpfc_terminate_rport_io,
2601
2602         .vport_disable = lpfc_vport_disable,
2603 };
2604
2605 void
2606 lpfc_get_cfgparam(struct lpfc_hba *phba)
2607 {
2608         lpfc_cr_delay_init(phba, lpfc_cr_delay);
2609         lpfc_cr_count_init(phba, lpfc_cr_count);
2610         lpfc_multi_ring_support_init(phba, lpfc_multi_ring_support);
2611         lpfc_multi_ring_rctl_init(phba, lpfc_multi_ring_rctl);
2612         lpfc_multi_ring_type_init(phba, lpfc_multi_ring_type);
2613         lpfc_ack0_init(phba, lpfc_ack0);
2614         lpfc_topology_init(phba, lpfc_topology);
2615         lpfc_link_speed_init(phba, lpfc_link_speed);
2616         lpfc_poll_tmo_init(phba, lpfc_poll_tmo);
2617         lpfc_enable_npiv_init(phba, lpfc_enable_npiv);
2618         lpfc_use_msi_init(phba, lpfc_use_msi);
2619         lpfc_enable_hba_reset_init(phba, lpfc_enable_hba_reset);
2620         lpfc_enable_hba_heartbeat_init(phba, lpfc_enable_hba_heartbeat);
2621         phba->cfg_poll = lpfc_poll;
2622         phba->cfg_soft_wwnn = 0L;
2623         phba->cfg_soft_wwpn = 0L;
2624         lpfc_sg_seg_cnt_init(phba, lpfc_sg_seg_cnt);
2625         /* Also reinitialize the host templates with new values. */
2626         lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
2627         lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
2628         /*
2629          * Since the sg_tablesize is module parameter, the sg_dma_buf_size
2630          * used to create the sg_dma_buf_pool must be dynamically calculated.
2631          * 2 segments are added since the IOCB needs a command and response bde.
2632          */
2633         phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
2634                         sizeof(struct fcp_rsp) +
2635                         ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
2636         lpfc_hba_queue_depth_init(phba, lpfc_hba_queue_depth);
2637         return;
2638 }
2639
2640 void
2641 lpfc_get_vport_cfgparam(struct lpfc_vport *vport)
2642 {
2643         lpfc_log_verbose_init(vport, lpfc_log_verbose);
2644         lpfc_lun_queue_depth_init(vport, lpfc_lun_queue_depth);
2645         lpfc_devloss_tmo_init(vport, lpfc_devloss_tmo);
2646         lpfc_nodev_tmo_init(vport, lpfc_nodev_tmo);
2647         lpfc_peer_port_login_init(vport, lpfc_peer_port_login);
2648         lpfc_restrict_login_init(vport, lpfc_restrict_login);
2649         lpfc_fcp_class_init(vport, lpfc_fcp_class);
2650         lpfc_use_adisc_init(vport, lpfc_use_adisc);
2651         lpfc_fdmi_on_init(vport, lpfc_fdmi_on);
2652         lpfc_discovery_threads_init(vport, lpfc_discovery_threads);
2653         lpfc_max_luns_init(vport, lpfc_max_luns);
2654         lpfc_scan_down_init(vport, lpfc_scan_down);
2655         lpfc_enable_da_id_init(vport, lpfc_enable_da_id);
2656         return;
2657 }