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