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