]> nv-tegra.nvidia Code Review - linux-3.10.git/blob - drivers/scsi/lpfc/lpfc_attr.c
[SCSI] lpfc 8.1.12 : Improve diagnostic messages and change local loopback message...
[linux-3.10.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-2006 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
43 #define LPFC_DEF_DEVLOSS_TMO 30
44 #define LPFC_MIN_DEVLOSS_TMO 1
45 #define LPFC_MAX_DEVLOSS_TMO 255
46
47 static void
48 lpfc_jedec_to_ascii(int incr, char hdw[])
49 {
50         int i, j;
51         for (i = 0; i < 8; i++) {
52                 j = (incr & 0xf);
53                 if (j <= 9)
54                         hdw[7 - i] = 0x30 +  j;
55                  else
56                         hdw[7 - i] = 0x61 + j - 10;
57                 incr = (incr >> 4);
58         }
59         hdw[8] = 0;
60         return;
61 }
62
63 static ssize_t
64 lpfc_drvr_version_show(struct class_device *cdev, char *buf)
65 {
66         return snprintf(buf, PAGE_SIZE, LPFC_MODULE_DESC "\n");
67 }
68
69 static ssize_t
70 management_version_show(struct class_device *cdev, char *buf)
71 {
72         return snprintf(buf, PAGE_SIZE, DFC_API_VERSION "\n");
73 }
74
75 static ssize_t
76 lpfc_info_show(struct class_device *cdev, char *buf)
77 {
78         struct Scsi_Host *host = class_to_shost(cdev);
79         return snprintf(buf, PAGE_SIZE, "%s\n",lpfc_info(host));
80 }
81
82 static ssize_t
83 lpfc_serialnum_show(struct class_device *cdev, char *buf)
84 {
85         struct Scsi_Host *host = class_to_shost(cdev);
86         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
87         return snprintf(buf, PAGE_SIZE, "%s\n",phba->SerialNumber);
88 }
89
90 static ssize_t
91 lpfc_modeldesc_show(struct class_device *cdev, char *buf)
92 {
93         struct Scsi_Host *host = class_to_shost(cdev);
94         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
95         return snprintf(buf, PAGE_SIZE, "%s\n",phba->ModelDesc);
96 }
97
98 static ssize_t
99 lpfc_modelname_show(struct class_device *cdev, char *buf)
100 {
101         struct Scsi_Host *host = class_to_shost(cdev);
102         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
103         return snprintf(buf, PAGE_SIZE, "%s\n",phba->ModelName);
104 }
105
106 static ssize_t
107 lpfc_programtype_show(struct class_device *cdev, char *buf)
108 {
109         struct Scsi_Host *host = class_to_shost(cdev);
110         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
111         return snprintf(buf, PAGE_SIZE, "%s\n",phba->ProgramType);
112 }
113
114 static ssize_t
115 lpfc_portnum_show(struct class_device *cdev, char *buf)
116 {
117         struct Scsi_Host *host = class_to_shost(cdev);
118         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
119         return snprintf(buf, PAGE_SIZE, "%s\n",phba->Port);
120 }
121
122 static ssize_t
123 lpfc_fwrev_show(struct class_device *cdev, char *buf)
124 {
125         struct Scsi_Host *host = class_to_shost(cdev);
126         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
127         char fwrev[32];
128         lpfc_decode_firmware_rev(phba, fwrev, 1);
129         return snprintf(buf, PAGE_SIZE, "%s\n",fwrev);
130 }
131
132 static ssize_t
133 lpfc_hdw_show(struct class_device *cdev, char *buf)
134 {
135         char hdw[9];
136         struct Scsi_Host *host = class_to_shost(cdev);
137         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
138         lpfc_vpd_t *vp = &phba->vpd;
139         lpfc_jedec_to_ascii(vp->rev.biuRev, hdw);
140         return snprintf(buf, PAGE_SIZE, "%s\n", hdw);
141 }
142 static ssize_t
143 lpfc_option_rom_version_show(struct class_device *cdev, char *buf)
144 {
145         struct Scsi_Host *host = class_to_shost(cdev);
146         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
147         return snprintf(buf, PAGE_SIZE, "%s\n", phba->OptionROMVersion);
148 }
149 static ssize_t
150 lpfc_state_show(struct class_device *cdev, char *buf)
151 {
152         struct Scsi_Host *host = class_to_shost(cdev);
153         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
154         int len = 0;
155         switch (phba->hba_state) {
156         case LPFC_STATE_UNKNOWN:
157         case LPFC_WARM_START:
158         case LPFC_INIT_START:
159         case LPFC_INIT_MBX_CMDS:
160         case LPFC_LINK_DOWN:
161                 len += snprintf(buf + len, PAGE_SIZE-len, "Link Down\n");
162                 break;
163         case LPFC_LINK_UP:
164         case LPFC_LOCAL_CFG_LINK:
165                 len += snprintf(buf + len, PAGE_SIZE-len, "Link Up\n");
166                 break;
167         case LPFC_FLOGI:
168         case LPFC_FABRIC_CFG_LINK:
169         case LPFC_NS_REG:
170         case LPFC_NS_QRY:
171         case LPFC_BUILD_DISC_LIST:
172         case LPFC_DISC_AUTH:
173         case LPFC_CLEAR_LA:
174                 len += snprintf(buf + len, PAGE_SIZE-len,
175                                 "Link Up - Discovery\n");
176                 break;
177         case LPFC_HBA_READY:
178                 len += snprintf(buf + len, PAGE_SIZE-len,
179                                 "Link Up - Ready:\n");
180                 if (phba->fc_topology == TOPOLOGY_LOOP) {
181                         if (phba->fc_flag & FC_PUBLIC_LOOP)
182                                 len += snprintf(buf + len, PAGE_SIZE-len,
183                                                 "   Public Loop\n");
184                         else
185                                 len += snprintf(buf + len, PAGE_SIZE-len,
186                                                 "   Private Loop\n");
187                 } else {
188                         if (phba->fc_flag & FC_FABRIC)
189                                 len += snprintf(buf + len, PAGE_SIZE-len,
190                                                 "   Fabric\n");
191                         else
192                                 len += snprintf(buf + len, PAGE_SIZE-len,
193                                                 "   Point-2-Point\n");
194                 }
195         }
196         return len;
197 }
198
199 static ssize_t
200 lpfc_num_discovered_ports_show(struct class_device *cdev, char *buf)
201 {
202         struct Scsi_Host *host = class_to_shost(cdev);
203         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
204         return snprintf(buf, PAGE_SIZE, "%d\n", phba->fc_map_cnt +
205                                                         phba->fc_unmap_cnt);
206 }
207
208
209 static int
210 lpfc_issue_lip(struct Scsi_Host *host)
211 {
212         struct lpfc_hba *phba = (struct lpfc_hba *) host->hostdata;
213         LPFC_MBOXQ_t *pmboxq;
214         int mbxstatus = MBXERR_ERROR;
215
216         if ((phba->fc_flag & FC_OFFLINE_MODE) ||
217             (phba->fc_flag & FC_BLOCK_MGMT_IO) ||
218             (phba->hba_state != LPFC_HBA_READY))
219                 return -EPERM;
220
221         pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
222
223         if (!pmboxq)
224                 return -ENOMEM;
225
226         memset((void *)pmboxq, 0, sizeof (LPFC_MBOXQ_t));
227         pmboxq->mb.mbxCommand = MBX_DOWN_LINK;
228         pmboxq->mb.mbxOwner = OWN_HOST;
229
230         mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO * 2);
231
232         if ((mbxstatus == MBX_SUCCESS) && (pmboxq->mb.mbxStatus == 0)) {
233                 memset((void *)pmboxq, 0, sizeof (LPFC_MBOXQ_t));
234                 lpfc_init_link(phba, pmboxq, phba->cfg_topology,
235                                phba->cfg_link_speed);
236                 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
237                                                      phba->fc_ratov * 2);
238         }
239
240         lpfc_set_loopback_flag(phba);
241         if (mbxstatus == MBX_TIMEOUT)
242                 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
243         else
244                 mempool_free(pmboxq, phba->mbox_mem_pool);
245
246         if (mbxstatus == MBXERR_ERROR)
247                 return -EIO;
248
249         return 0;
250 }
251
252 static int
253 lpfc_do_offline(struct lpfc_hba *phba, uint32_t type)
254 {
255         struct completion online_compl;
256         struct lpfc_sli_ring *pring;
257         struct lpfc_sli *psli;
258         int status = 0;
259         int cnt = 0;
260         int i;
261
262         init_completion(&online_compl);
263         lpfc_workq_post_event(phba, &status, &online_compl,
264                               LPFC_EVT_OFFLINE_PREP);
265         wait_for_completion(&online_compl);
266
267         if (status != 0)
268                 return -EIO;
269
270         psli = &phba->sli;
271
272         for (i = 0; i < psli->num_rings; i++) {
273                 pring = &psli->ring[i];
274                 /* The linkdown event takes 30 seconds to timeout. */
275                 while (pring->txcmplq_cnt) {
276                         msleep(10);
277                         if (cnt++ > 3000) {
278                                 lpfc_printf_log(phba,
279                                         KERN_WARNING, LOG_INIT,
280                                         "%d:0466 Outstanding IO when "
281                                         "bringing Adapter offline\n",
282                                         phba->brd_no);
283                                 break;
284                         }
285                 }
286         }
287
288         init_completion(&online_compl);
289         lpfc_workq_post_event(phba, &status, &online_compl, type);
290         wait_for_completion(&online_compl);
291
292         if (status != 0)
293                 return -EIO;
294
295         return 0;
296 }
297
298 static int
299 lpfc_selective_reset(struct lpfc_hba *phba)
300 {
301         struct completion online_compl;
302         int status = 0;
303
304         status = lpfc_do_offline(phba, LPFC_EVT_OFFLINE);
305
306         if (status != 0)
307                 return status;
308
309         init_completion(&online_compl);
310         lpfc_workq_post_event(phba, &status, &online_compl,
311                               LPFC_EVT_ONLINE);
312         wait_for_completion(&online_compl);
313
314         if (status != 0)
315                 return -EIO;
316
317         return 0;
318 }
319
320 static ssize_t
321 lpfc_issue_reset(struct class_device *cdev, const char *buf, size_t count)
322 {
323         struct Scsi_Host *host = class_to_shost(cdev);
324         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
325         int status = -EINVAL;
326
327         if (strncmp(buf, "selective", sizeof("selective") - 1) == 0)
328                 status = lpfc_selective_reset(phba);
329
330         if (status == 0)
331                 return strlen(buf);
332         else
333                 return status;
334 }
335
336 static ssize_t
337 lpfc_nport_evt_cnt_show(struct class_device *cdev, char *buf)
338 {
339         struct Scsi_Host *host = class_to_shost(cdev);
340         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
341         return snprintf(buf, PAGE_SIZE, "%d\n", phba->nport_event_cnt);
342 }
343
344 static ssize_t
345 lpfc_board_mode_show(struct class_device *cdev, char *buf)
346 {
347         struct Scsi_Host *host = class_to_shost(cdev);
348         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
349         char  * state;
350
351         if (phba->hba_state == LPFC_HBA_ERROR)
352                 state = "error";
353         else if (phba->hba_state == LPFC_WARM_START)
354                 state = "warm start";
355         else if (phba->hba_state == LPFC_INIT_START)
356                 state = "offline";
357         else
358                 state = "online";
359
360         return snprintf(buf, PAGE_SIZE, "%s\n", state);
361 }
362
363 static ssize_t
364 lpfc_board_mode_store(struct class_device *cdev, const char *buf, size_t count)
365 {
366         struct Scsi_Host *host = class_to_shost(cdev);
367         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
368         struct completion online_compl;
369         int status=0;
370
371         init_completion(&online_compl);
372
373         if(strncmp(buf, "online", sizeof("online") - 1) == 0) {
374                 lpfc_workq_post_event(phba, &status, &online_compl,
375                                       LPFC_EVT_ONLINE);
376                 wait_for_completion(&online_compl);
377         } else if (strncmp(buf, "offline", sizeof("offline") - 1) == 0)
378                 status = lpfc_do_offline(phba, LPFC_EVT_OFFLINE);
379         else if (strncmp(buf, "warm", sizeof("warm") - 1) == 0)
380                 status = lpfc_do_offline(phba, LPFC_EVT_WARM_START);
381         else if (strncmp(buf, "error", sizeof("error") - 1) == 0)
382                 status = lpfc_do_offline(phba, LPFC_EVT_KILL);
383         else
384                 return -EINVAL;
385
386         if (!status)
387                 return strlen(buf);
388         else
389                 return -EIO;
390 }
391
392 static ssize_t
393 lpfc_poll_show(struct class_device *cdev, char *buf)
394 {
395         struct Scsi_Host *host = class_to_shost(cdev);
396         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
397
398         return snprintf(buf, PAGE_SIZE, "%#x\n", phba->cfg_poll);
399 }
400
401 static ssize_t
402 lpfc_poll_store(struct class_device *cdev, const char *buf,
403                 size_t count)
404 {
405         struct Scsi_Host *host = class_to_shost(cdev);
406         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
407         uint32_t creg_val;
408         uint32_t old_val;
409         int val=0;
410
411         if (!isdigit(buf[0]))
412                 return -EINVAL;
413
414         if (sscanf(buf, "%i", &val) != 1)
415                 return -EINVAL;
416
417         if ((val & 0x3) != val)
418                 return -EINVAL;
419
420         spin_lock_irq(phba->host->host_lock);
421
422         old_val = phba->cfg_poll;
423
424         if (val & ENABLE_FCP_RING_POLLING) {
425                 if ((val & DISABLE_FCP_RING_INT) &&
426                     !(old_val & DISABLE_FCP_RING_INT)) {
427                         creg_val = readl(phba->HCregaddr);
428                         creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
429                         writel(creg_val, phba->HCregaddr);
430                         readl(phba->HCregaddr); /* flush */
431
432                         lpfc_poll_start_timer(phba);
433                 }
434         } else if (val != 0x0) {
435                 spin_unlock_irq(phba->host->host_lock);
436                 return -EINVAL;
437         }
438
439         if (!(val & DISABLE_FCP_RING_INT) &&
440             (old_val & DISABLE_FCP_RING_INT))
441         {
442                 spin_unlock_irq(phba->host->host_lock);
443                 del_timer(&phba->fcp_poll_timer);
444                 spin_lock_irq(phba->host->host_lock);
445                 creg_val = readl(phba->HCregaddr);
446                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
447                 writel(creg_val, phba->HCregaddr);
448                 readl(phba->HCregaddr); /* flush */
449         }
450
451         phba->cfg_poll = val;
452
453         spin_unlock_irq(phba->host->host_lock);
454
455         return strlen(buf);
456 }
457
458 #define lpfc_param_show(attr)   \
459 static ssize_t \
460 lpfc_##attr##_show(struct class_device *cdev, char *buf) \
461 { \
462         struct Scsi_Host *host = class_to_shost(cdev);\
463         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;\
464         int val = 0;\
465         val = phba->cfg_##attr;\
466         return snprintf(buf, PAGE_SIZE, "%d\n",\
467                         phba->cfg_##attr);\
468 }
469
470 #define lpfc_param_hex_show(attr)       \
471 static ssize_t \
472 lpfc_##attr##_show(struct class_device *cdev, char *buf) \
473 { \
474         struct Scsi_Host *host = class_to_shost(cdev);\
475         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;\
476         int val = 0;\
477         val = phba->cfg_##attr;\
478         return snprintf(buf, PAGE_SIZE, "%#x\n",\
479                         phba->cfg_##attr);\
480 }
481
482 #define lpfc_param_init(attr, default, minval, maxval)  \
483 static int \
484 lpfc_##attr##_init(struct lpfc_hba *phba, int val) \
485 { \
486         if (val >= minval && val <= maxval) {\
487                 phba->cfg_##attr = val;\
488                 return 0;\
489         }\
490         lpfc_printf_log(phba, KERN_ERR, LOG_INIT, \
491                         "%d:0449 lpfc_"#attr" attribute cannot be set to %d, "\
492                         "allowed range is ["#minval", "#maxval"]\n", \
493                         phba->brd_no, val); \
494         phba->cfg_##attr = default;\
495         return -EINVAL;\
496 }
497
498 #define lpfc_param_set(attr, default, minval, maxval)   \
499 static int \
500 lpfc_##attr##_set(struct lpfc_hba *phba, int val) \
501 { \
502         if (val >= minval && val <= maxval) {\
503                 phba->cfg_##attr = val;\
504                 return 0;\
505         }\
506         lpfc_printf_log(phba, KERN_ERR, LOG_INIT, \
507                         "%d:0450 lpfc_"#attr" attribute cannot be set to %d, "\
508                         "allowed range is ["#minval", "#maxval"]\n", \
509                         phba->brd_no, val); \
510         return -EINVAL;\
511 }
512
513 #define lpfc_param_store(attr)  \
514 static ssize_t \
515 lpfc_##attr##_store(struct class_device *cdev, const char *buf, size_t count) \
516 { \
517         struct Scsi_Host *host = class_to_shost(cdev);\
518         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;\
519         int val=0;\
520         if (!isdigit(buf[0]))\
521                 return -EINVAL;\
522         if (sscanf(buf, "%i", &val) != 1)\
523                 return -EINVAL;\
524         if (lpfc_##attr##_set(phba, val) == 0) \
525                 return strlen(buf);\
526         else \
527                 return -EINVAL;\
528 }
529
530 #define LPFC_ATTR(name, defval, minval, maxval, desc) \
531 static int lpfc_##name = defval;\
532 module_param(lpfc_##name, int, 0);\
533 MODULE_PARM_DESC(lpfc_##name, desc);\
534 lpfc_param_init(name, defval, minval, maxval)
535
536 #define LPFC_ATTR_R(name, defval, minval, maxval, desc) \
537 static int lpfc_##name = defval;\
538 module_param(lpfc_##name, int, 0);\
539 MODULE_PARM_DESC(lpfc_##name, desc);\
540 lpfc_param_show(name)\
541 lpfc_param_init(name, defval, minval, maxval)\
542 static CLASS_DEVICE_ATTR(lpfc_##name, S_IRUGO , lpfc_##name##_show, NULL)
543
544 #define LPFC_ATTR_RW(name, defval, minval, maxval, desc) \
545 static int lpfc_##name = defval;\
546 module_param(lpfc_##name, int, 0);\
547 MODULE_PARM_DESC(lpfc_##name, desc);\
548 lpfc_param_show(name)\
549 lpfc_param_init(name, defval, minval, maxval)\
550 lpfc_param_set(name, defval, minval, maxval)\
551 lpfc_param_store(name)\
552 static CLASS_DEVICE_ATTR(lpfc_##name, S_IRUGO | S_IWUSR,\
553                          lpfc_##name##_show, lpfc_##name##_store)
554
555 #define LPFC_ATTR_HEX_R(name, defval, minval, maxval, desc) \
556 static int lpfc_##name = defval;\
557 module_param(lpfc_##name, int, 0);\
558 MODULE_PARM_DESC(lpfc_##name, desc);\
559 lpfc_param_hex_show(name)\
560 lpfc_param_init(name, defval, minval, maxval)\
561 static CLASS_DEVICE_ATTR(lpfc_##name, S_IRUGO , lpfc_##name##_show, NULL)
562
563 #define LPFC_ATTR_HEX_RW(name, defval, minval, maxval, desc) \
564 static int lpfc_##name = defval;\
565 module_param(lpfc_##name, int, 0);\
566 MODULE_PARM_DESC(lpfc_##name, desc);\
567 lpfc_param_hex_show(name)\
568 lpfc_param_init(name, defval, minval, maxval)\
569 lpfc_param_set(name, defval, minval, maxval)\
570 lpfc_param_store(name)\
571 static CLASS_DEVICE_ATTR(lpfc_##name, S_IRUGO | S_IWUSR,\
572                          lpfc_##name##_show, lpfc_##name##_store)
573
574 static CLASS_DEVICE_ATTR(info, S_IRUGO, lpfc_info_show, NULL);
575 static CLASS_DEVICE_ATTR(serialnum, S_IRUGO, lpfc_serialnum_show, NULL);
576 static CLASS_DEVICE_ATTR(modeldesc, S_IRUGO, lpfc_modeldesc_show, NULL);
577 static CLASS_DEVICE_ATTR(modelname, S_IRUGO, lpfc_modelname_show, NULL);
578 static CLASS_DEVICE_ATTR(programtype, S_IRUGO, lpfc_programtype_show, NULL);
579 static CLASS_DEVICE_ATTR(portnum, S_IRUGO, lpfc_portnum_show, NULL);
580 static CLASS_DEVICE_ATTR(fwrev, S_IRUGO, lpfc_fwrev_show, NULL);
581 static CLASS_DEVICE_ATTR(hdw, S_IRUGO, lpfc_hdw_show, NULL);
582 static CLASS_DEVICE_ATTR(state, S_IRUGO, lpfc_state_show, NULL);
583 static CLASS_DEVICE_ATTR(option_rom_version, S_IRUGO,
584                                         lpfc_option_rom_version_show, NULL);
585 static CLASS_DEVICE_ATTR(num_discovered_ports, S_IRUGO,
586                                         lpfc_num_discovered_ports_show, NULL);
587 static CLASS_DEVICE_ATTR(nport_evt_cnt, S_IRUGO, lpfc_nport_evt_cnt_show, NULL);
588 static CLASS_DEVICE_ATTR(lpfc_drvr_version, S_IRUGO, lpfc_drvr_version_show,
589                          NULL);
590 static CLASS_DEVICE_ATTR(management_version, S_IRUGO, management_version_show,
591                          NULL);
592 static CLASS_DEVICE_ATTR(board_mode, S_IRUGO | S_IWUSR,
593                          lpfc_board_mode_show, lpfc_board_mode_store);
594 static CLASS_DEVICE_ATTR(issue_reset, S_IWUSR, NULL, lpfc_issue_reset);
595
596
597 static char *lpfc_soft_wwn_key = "C99G71SL8032A";
598
599 static ssize_t
600 lpfc_soft_wwn_enable_store(struct class_device *cdev, const char *buf,
601                                 size_t count)
602 {
603         struct Scsi_Host *host = class_to_shost(cdev);
604         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
605         unsigned int cnt = count;
606
607         /*
608          * We're doing a simple sanity check for soft_wwpn setting.
609          * We require that the user write a specific key to enable
610          * the soft_wwpn attribute to be settable. Once the attribute
611          * is written, the enable key resets. If further updates are
612          * desired, the key must be written again to re-enable the
613          * attribute.
614          *
615          * The "key" is not secret - it is a hardcoded string shown
616          * here. The intent is to protect against the random user or
617          * application that is just writing attributes.
618          */
619
620         /* count may include a LF at end of string */
621         if (buf[cnt-1] == '\n')
622                 cnt--;
623
624         if ((cnt != strlen(lpfc_soft_wwn_key)) ||
625             (strncmp(buf, lpfc_soft_wwn_key, strlen(lpfc_soft_wwn_key)) != 0))
626                 return -EINVAL;
627
628         phba->soft_wwn_enable = 1;
629         return count;
630 }
631 static CLASS_DEVICE_ATTR(lpfc_soft_wwn_enable, S_IWUSR, NULL,
632                                 lpfc_soft_wwn_enable_store);
633
634 static ssize_t
635 lpfc_soft_wwpn_show(struct class_device *cdev, char *buf)
636 {
637         struct Scsi_Host *host = class_to_shost(cdev);
638         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
639         return snprintf(buf, PAGE_SIZE, "0x%llx\n",
640                         (unsigned long long)phba->cfg_soft_wwpn);
641 }
642
643
644 static ssize_t
645 lpfc_soft_wwpn_store(struct class_device *cdev, const char *buf, size_t count)
646 {
647         struct Scsi_Host *host = class_to_shost(cdev);
648         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
649         struct completion online_compl;
650         int stat1=0, stat2=0;
651         unsigned int i, j, cnt=count;
652         u8 wwpn[8];
653
654         /* count may include a LF at end of string */
655         if (buf[cnt-1] == '\n')
656                 cnt--;
657
658         if (!phba->soft_wwn_enable || (cnt < 16) || (cnt > 18) ||
659             ((cnt == 17) && (*buf++ != 'x')) ||
660             ((cnt == 18) && ((*buf++ != '0') || (*buf++ != 'x'))))
661                 return -EINVAL;
662
663         phba->soft_wwn_enable = 0;
664
665         memset(wwpn, 0, sizeof(wwpn));
666
667         /* Validate and store the new name */
668         for (i=0, j=0; i < 16; i++) {
669                 if ((*buf >= 'a') && (*buf <= 'f'))
670                         j = ((j << 4) | ((*buf++ -'a') + 10));
671                 else if ((*buf >= 'A') && (*buf <= 'F'))
672                         j = ((j << 4) | ((*buf++ -'A') + 10));
673                 else if ((*buf >= '0') && (*buf <= '9'))
674                         j = ((j << 4) | (*buf++ -'0'));
675                 else
676                         return -EINVAL;
677                 if (i % 2) {
678                         wwpn[i/2] = j & 0xff;
679                         j = 0;
680                 }
681         }
682         phba->cfg_soft_wwpn = wwn_to_u64(wwpn);
683         fc_host_port_name(host) = phba->cfg_soft_wwpn;
684         if (phba->cfg_soft_wwnn)
685                 fc_host_node_name(host) = phba->cfg_soft_wwnn;
686
687         dev_printk(KERN_NOTICE, &phba->pcidev->dev,
688                    "lpfc%d: Reinitializing to use soft_wwpn\n", phba->brd_no);
689
690         stat1 = lpfc_do_offline(phba, LPFC_EVT_OFFLINE);
691         if (stat1)
692                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
693                         "%d:0463 lpfc_soft_wwpn attribute set failed to reinit "
694                         "adapter - %d\n", phba->brd_no, stat1);
695
696         init_completion(&online_compl);
697         lpfc_workq_post_event(phba, &stat2, &online_compl, LPFC_EVT_ONLINE);
698         wait_for_completion(&online_compl);
699         if (stat2)
700                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
701                         "%d:0464 lpfc_soft_wwpn attribute set failed to reinit "
702                         "adapter - %d\n", phba->brd_no, stat2);
703
704         return (stat1 || stat2) ? -EIO : count;
705 }
706 static CLASS_DEVICE_ATTR(lpfc_soft_wwpn, S_IRUGO | S_IWUSR,\
707                          lpfc_soft_wwpn_show, lpfc_soft_wwpn_store);
708
709 static ssize_t
710 lpfc_soft_wwnn_show(struct class_device *cdev, char *buf)
711 {
712         struct Scsi_Host *host = class_to_shost(cdev);
713         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
714         return snprintf(buf, PAGE_SIZE, "0x%llx\n",
715                         (unsigned long long)phba->cfg_soft_wwnn);
716 }
717
718
719 static ssize_t
720 lpfc_soft_wwnn_store(struct class_device *cdev, const char *buf, size_t count)
721 {
722         struct Scsi_Host *host = class_to_shost(cdev);
723         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
724         unsigned int i, j, cnt=count;
725         u8 wwnn[8];
726
727         /* count may include a LF at end of string */
728         if (buf[cnt-1] == '\n')
729                 cnt--;
730
731         if (!phba->soft_wwn_enable || (cnt < 16) || (cnt > 18) ||
732             ((cnt == 17) && (*buf++ != 'x')) ||
733             ((cnt == 18) && ((*buf++ != '0') || (*buf++ != 'x'))))
734                 return -EINVAL;
735
736         /*
737          * Allow wwnn to be set many times, as long as the enable is set.
738          * However, once the wwpn is set, everything locks.
739          */
740
741         memset(wwnn, 0, sizeof(wwnn));
742
743         /* Validate and store the new name */
744         for (i=0, j=0; i < 16; i++) {
745                 if ((*buf >= 'a') && (*buf <= 'f'))
746                         j = ((j << 4) | ((*buf++ -'a') + 10));
747                 else if ((*buf >= 'A') && (*buf <= 'F'))
748                         j = ((j << 4) | ((*buf++ -'A') + 10));
749                 else if ((*buf >= '0') && (*buf <= '9'))
750                         j = ((j << 4) | (*buf++ -'0'));
751                 else
752                         return -EINVAL;
753                 if (i % 2) {
754                         wwnn[i/2] = j & 0xff;
755                         j = 0;
756                 }
757         }
758         phba->cfg_soft_wwnn = wwn_to_u64(wwnn);
759
760         dev_printk(KERN_NOTICE, &phba->pcidev->dev,
761                    "lpfc%d: soft_wwnn set. Value will take effect upon "
762                    "setting of the soft_wwpn\n", phba->brd_no);
763
764         return count;
765 }
766 static CLASS_DEVICE_ATTR(lpfc_soft_wwnn, S_IRUGO | S_IWUSR,\
767                          lpfc_soft_wwnn_show, lpfc_soft_wwnn_store);
768
769
770 static int lpfc_poll = 0;
771 module_param(lpfc_poll, int, 0);
772 MODULE_PARM_DESC(lpfc_poll, "FCP ring polling mode control:"
773                  " 0 - none,"
774                  " 1 - poll with interrupts enabled"
775                  " 3 - poll and disable FCP ring interrupts");
776
777 static CLASS_DEVICE_ATTR(lpfc_poll, S_IRUGO | S_IWUSR,
778                          lpfc_poll_show, lpfc_poll_store);
779
780 /*
781 # lpfc_nodev_tmo: If set, it will hold all I/O errors on devices that disappear
782 # until the timer expires. Value range is [0,255]. Default value is 30.
783 */
784 static int lpfc_nodev_tmo = LPFC_DEF_DEVLOSS_TMO;
785 static int lpfc_devloss_tmo = LPFC_DEF_DEVLOSS_TMO;
786 module_param(lpfc_nodev_tmo, int, 0);
787 MODULE_PARM_DESC(lpfc_nodev_tmo,
788                  "Seconds driver will hold I/O waiting "
789                  "for a device to come back");
790 static ssize_t
791 lpfc_nodev_tmo_show(struct class_device *cdev, char *buf)
792 {
793         struct Scsi_Host *host = class_to_shost(cdev);
794         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
795         int val = 0;
796         val = phba->cfg_devloss_tmo;
797         return snprintf(buf, PAGE_SIZE, "%d\n",
798                         phba->cfg_devloss_tmo);
799 }
800
801 static int
802 lpfc_nodev_tmo_init(struct lpfc_hba *phba, int val)
803 {
804         static int warned;
805         if (phba->cfg_devloss_tmo !=  LPFC_DEF_DEVLOSS_TMO) {
806                 phba->cfg_nodev_tmo = phba->cfg_devloss_tmo;
807                 if (!warned && val != LPFC_DEF_DEVLOSS_TMO) {
808                         warned = 1;
809                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
810                                         "%d:0402 Ignoring nodev_tmo module "
811                                         "parameter because devloss_tmo is"
812                                         " set.\n",
813                                         phba->brd_no);
814                 }
815                 return 0;
816         }
817
818         if (val >= LPFC_MIN_DEVLOSS_TMO && val <= LPFC_MAX_DEVLOSS_TMO) {
819                 phba->cfg_nodev_tmo = val;
820                 phba->cfg_devloss_tmo = val;
821                 return 0;
822         }
823         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
824                         "%d:0400 lpfc_nodev_tmo attribute cannot be set to %d, "
825                         "allowed range is [%d, %d]\n",
826                         phba->brd_no, val,
827                         LPFC_MIN_DEVLOSS_TMO, LPFC_MAX_DEVLOSS_TMO);
828         phba->cfg_nodev_tmo = LPFC_DEF_DEVLOSS_TMO;
829         return -EINVAL;
830 }
831
832 static void
833 lpfc_update_rport_devloss_tmo(struct lpfc_hba *phba)
834 {
835         struct lpfc_nodelist  *ndlp;
836
837         spin_lock_irq(phba->host->host_lock);
838         list_for_each_entry(ndlp, &phba->fc_nodes, nlp_listp)
839                 if (ndlp->rport)
840                         ndlp->rport->dev_loss_tmo = phba->cfg_devloss_tmo;
841         spin_unlock_irq(phba->host->host_lock);
842 }
843
844 static int
845 lpfc_nodev_tmo_set(struct lpfc_hba *phba, int val)
846 {
847         if (phba->dev_loss_tmo_changed ||
848                 (lpfc_devloss_tmo != LPFC_DEF_DEVLOSS_TMO)) {
849                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
850                                 "%d:0401 Ignoring change to nodev_tmo "
851                                 "because devloss_tmo is set.\n",
852                                 phba->brd_no);
853                 return 0;
854         }
855
856         if (val >= LPFC_MIN_DEVLOSS_TMO && val <= LPFC_MAX_DEVLOSS_TMO) {
857                 phba->cfg_nodev_tmo = val;
858                 phba->cfg_devloss_tmo = val;
859                 lpfc_update_rport_devloss_tmo(phba);
860                 return 0;
861         }
862
863         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
864                         "%d:0403 lpfc_nodev_tmo attribute cannot be set to %d, "
865                         "allowed range is [%d, %d]\n",
866                         phba->brd_no, val, LPFC_MIN_DEVLOSS_TMO,
867                         LPFC_MAX_DEVLOSS_TMO);
868         return -EINVAL;
869 }
870
871 lpfc_param_store(nodev_tmo)
872
873 static CLASS_DEVICE_ATTR(lpfc_nodev_tmo, S_IRUGO | S_IWUSR,
874                          lpfc_nodev_tmo_show, lpfc_nodev_tmo_store);
875
876 /*
877 # lpfc_devloss_tmo: If set, it will hold all I/O errors on devices that
878 # disappear until the timer expires. Value range is [0,255]. Default
879 # value is 30.
880 */
881 module_param(lpfc_devloss_tmo, int, 0);
882 MODULE_PARM_DESC(lpfc_devloss_tmo,
883                  "Seconds driver will hold I/O waiting "
884                  "for a device to come back");
885 lpfc_param_init(devloss_tmo, LPFC_DEF_DEVLOSS_TMO,
886                 LPFC_MIN_DEVLOSS_TMO, LPFC_MAX_DEVLOSS_TMO)
887 lpfc_param_show(devloss_tmo)
888 static int
889 lpfc_devloss_tmo_set(struct lpfc_hba *phba, int val)
890 {
891         if (val >= LPFC_MIN_DEVLOSS_TMO && val <= LPFC_MAX_DEVLOSS_TMO) {
892                 phba->cfg_nodev_tmo = val;
893                 phba->cfg_devloss_tmo = val;
894                 phba->dev_loss_tmo_changed = 1;
895                 lpfc_update_rport_devloss_tmo(phba);
896                 return 0;
897         }
898
899         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
900                         "%d:0404 lpfc_devloss_tmo attribute cannot be set to"
901                         " %d, allowed range is [%d, %d]\n",
902                         phba->brd_no, val, LPFC_MIN_DEVLOSS_TMO,
903                         LPFC_MAX_DEVLOSS_TMO);
904         return -EINVAL;
905 }
906
907 lpfc_param_store(devloss_tmo)
908 static CLASS_DEVICE_ATTR(lpfc_devloss_tmo, S_IRUGO | S_IWUSR,
909         lpfc_devloss_tmo_show, lpfc_devloss_tmo_store);
910
911 /*
912 # lpfc_log_verbose: Only turn this flag on if you are willing to risk being
913 # deluged with LOTS of information.
914 # You can set a bit mask to record specific types of verbose messages:
915 #
916 # LOG_ELS                       0x1        ELS events
917 # LOG_DISCOVERY                 0x2        Link discovery events
918 # LOG_MBOX                      0x4        Mailbox events
919 # LOG_INIT                      0x8        Initialization events
920 # LOG_LINK_EVENT                0x10       Link events
921 # LOG_FCP                       0x40       FCP traffic history
922 # LOG_NODE                      0x80       Node table events
923 # LOG_MISC                      0x400      Miscellaneous events
924 # LOG_SLI                       0x800      SLI events
925 # LOG_FCP_ERROR                 0x1000     Only log FCP errors
926 # LOG_LIBDFC                    0x2000     LIBDFC events
927 # LOG_ALL_MSG                   0xffff     LOG all messages
928 */
929 LPFC_ATTR_HEX_RW(log_verbose, 0x0, 0x0, 0xffff, "Verbose logging bit-mask");
930
931 /*
932 # lun_queue_depth:  This parameter is used to limit the number of outstanding
933 # commands per FCP LUN. Value range is [1,128]. Default value is 30.
934 */
935 LPFC_ATTR_R(lun_queue_depth, 30, 1, 128,
936             "Max number of FCP commands we can queue to a specific LUN");
937
938 /*
939 # hba_queue_depth:  This parameter is used to limit the number of outstanding
940 # commands per lpfc HBA. Value range is [32,8192]. If this parameter
941 # value is greater than the maximum number of exchanges supported by the HBA,
942 # then maximum number of exchanges supported by the HBA is used to determine
943 # the hba_queue_depth.
944 */
945 LPFC_ATTR_R(hba_queue_depth, 8192, 32, 8192,
946             "Max number of FCP commands we can queue to a lpfc HBA");
947
948 /*
949 # Some disk devices have a "select ID" or "select Target" capability.
950 # From a protocol standpoint "select ID" usually means select the
951 # Fibre channel "ALPA".  In the FC-AL Profile there is an "informative
952 # annex" which contains a table that maps a "select ID" (a number
953 # between 0 and 7F) to an ALPA.  By default, for compatibility with
954 # older drivers, the lpfc driver scans this table from low ALPA to high
955 # ALPA.
956 #
957 # Turning on the scan-down variable (on  = 1, off = 0) will
958 # cause the lpfc driver to use an inverted table, effectively
959 # scanning ALPAs from high to low. Value range is [0,1]. Default value is 1.
960 #
961 # (Note: This "select ID" functionality is a LOOP ONLY characteristic
962 # and will not work across a fabric. Also this parameter will take
963 # effect only in the case when ALPA map is not available.)
964 */
965 LPFC_ATTR_R(scan_down, 1, 0, 1,
966              "Start scanning for devices from highest ALPA to lowest");
967
968 /*
969 # lpfc_topology:  link topology for init link
970 #            0x0  = attempt loop mode then point-to-point
971 #            0x01 = internal loopback mode
972 #            0x02 = attempt point-to-point mode only
973 #            0x04 = attempt loop mode only
974 #            0x06 = attempt point-to-point mode then loop
975 # Set point-to-point mode if you want to run as an N_Port.
976 # Set loop mode if you want to run as an NL_Port. Value range is [0,0x6].
977 # Default value is 0.
978 */
979 LPFC_ATTR_RW(topology, 0, 0, 6, "Select Fibre Channel topology");
980
981 /*
982 # lpfc_link_speed: Link speed selection for initializing the Fibre Channel
983 # connection.
984 #       0  = auto select (default)
985 #       1  = 1 Gigabaud
986 #       2  = 2 Gigabaud
987 #       4  = 4 Gigabaud
988 # Value range is [0,4]. Default value is 0.
989 */
990 LPFC_ATTR_R(link_speed, 0, 0, 4, "Select link speed");
991
992 /*
993 # lpfc_fcp_class:  Determines FC class to use for the FCP protocol.
994 # Value range is [2,3]. Default value is 3.
995 */
996 LPFC_ATTR_R(fcp_class, 3, 2, 3,
997              "Select Fibre Channel class of service for FCP sequences");
998
999 /*
1000 # lpfc_use_adisc: Use ADISC for FCP rediscovery instead of PLOGI. Value range
1001 # is [0,1]. Default value is 0.
1002 */
1003 LPFC_ATTR_RW(use_adisc, 0, 0, 1,
1004              "Use ADISC on rediscovery to authenticate FCP devices");
1005
1006 /*
1007 # lpfc_ack0: Use ACK0, instead of ACK1 for class 2 acknowledgement. Value
1008 # range is [0,1]. Default value is 0.
1009 */
1010 LPFC_ATTR_R(ack0, 0, 0, 1, "Enable ACK0 support");
1011
1012 /*
1013 # lpfc_cr_delay & lpfc_cr_count: Default values for I/O colaesing
1014 # cr_delay (msec) or cr_count outstanding commands. cr_delay can take
1015 # value [0,63]. cr_count can take value [1,255]. Default value of cr_delay
1016 # is 0. Default value of cr_count is 1. The cr_count feature is disabled if
1017 # cr_delay is set to 0.
1018 */
1019 LPFC_ATTR_RW(cr_delay, 0, 0, 63, "A count of milliseconds after which an "
1020                 "interrupt response is generated");
1021
1022 LPFC_ATTR_RW(cr_count, 1, 1, 255, "A count of I/O completions after which an "
1023                 "interrupt response is generated");
1024
1025 /*
1026 # lpfc_multi_ring_support:  Determines how many rings to spread available
1027 # cmd/rsp IOCB entries across.
1028 # Value range is [1,2]. Default value is 1.
1029 */
1030 LPFC_ATTR_R(multi_ring_support, 1, 1, 2, "Determines number of primary "
1031                 "SLI rings to spread IOCB entries across");
1032
1033 /*
1034 # lpfc_multi_ring_rctl:  If lpfc_multi_ring_support is enabled, this
1035 # identifies what rctl value to configure the additional ring for.
1036 # Value range is [1,0xff]. Default value is 4 (Unsolicated Data).
1037 */
1038 LPFC_ATTR_R(multi_ring_rctl, FC_UNSOL_DATA, 1,
1039              255, "Identifies RCTL for additional ring configuration");
1040
1041 /*
1042 # lpfc_multi_ring_type:  If lpfc_multi_ring_support is enabled, this
1043 # identifies what type value to configure the additional ring for.
1044 # Value range is [1,0xff]. Default value is 5 (LLC/SNAP).
1045 */
1046 LPFC_ATTR_R(multi_ring_type, FC_LLC_SNAP, 1,
1047              255, "Identifies TYPE for additional ring configuration");
1048
1049 /*
1050 # lpfc_fdmi_on: controls FDMI support.
1051 #       0 = no FDMI support
1052 #       1 = support FDMI without attribute of hostname
1053 #       2 = support FDMI with attribute of hostname
1054 # Value range [0,2]. Default value is 0.
1055 */
1056 LPFC_ATTR_RW(fdmi_on, 0, 0, 2, "Enable FDMI support");
1057
1058 /*
1059 # Specifies the maximum number of ELS cmds we can have outstanding (for
1060 # discovery). Value range is [1,64]. Default value = 32.
1061 */
1062 LPFC_ATTR(discovery_threads, 32, 1, 64, "Maximum number of ELS commands "
1063                  "during discovery");
1064
1065 /*
1066 # lpfc_max_luns: maximum allowed LUN.
1067 # Value range is [0,65535]. Default value is 255.
1068 # NOTE: The SCSI layer might probe all allowed LUN on some old targets.
1069 */
1070 LPFC_ATTR_R(max_luns, 255, 0, 65535,
1071              "Maximum allowed LUN");
1072
1073 /*
1074 # lpfc_poll_tmo: .Milliseconds driver will wait between polling FCP ring.
1075 # Value range is [1,255], default value is 10.
1076 */
1077 LPFC_ATTR_RW(poll_tmo, 10, 1, 255,
1078              "Milliseconds driver will wait between polling FCP ring");
1079
1080 /*
1081 # lpfc_use_msi: Use MSI (Message Signaled Interrupts) in systems that
1082 #               support this feature
1083 #       0  = MSI disabled (default)
1084 #       1  = MSI enabled
1085 # Value range is [0,1]. Default value is 0.
1086 */
1087 LPFC_ATTR_R(use_msi, 0, 0, 1, "Use Message Signaled Interrupts, if possible");
1088
1089
1090 struct class_device_attribute *lpfc_host_attrs[] = {
1091         &class_device_attr_info,
1092         &class_device_attr_serialnum,
1093         &class_device_attr_modeldesc,
1094         &class_device_attr_modelname,
1095         &class_device_attr_programtype,
1096         &class_device_attr_portnum,
1097         &class_device_attr_fwrev,
1098         &class_device_attr_hdw,
1099         &class_device_attr_option_rom_version,
1100         &class_device_attr_state,
1101         &class_device_attr_num_discovered_ports,
1102         &class_device_attr_lpfc_drvr_version,
1103         &class_device_attr_lpfc_log_verbose,
1104         &class_device_attr_lpfc_lun_queue_depth,
1105         &class_device_attr_lpfc_hba_queue_depth,
1106         &class_device_attr_lpfc_nodev_tmo,
1107         &class_device_attr_lpfc_devloss_tmo,
1108         &class_device_attr_lpfc_fcp_class,
1109         &class_device_attr_lpfc_use_adisc,
1110         &class_device_attr_lpfc_ack0,
1111         &class_device_attr_lpfc_topology,
1112         &class_device_attr_lpfc_scan_down,
1113         &class_device_attr_lpfc_link_speed,
1114         &class_device_attr_lpfc_cr_delay,
1115         &class_device_attr_lpfc_cr_count,
1116         &class_device_attr_lpfc_multi_ring_support,
1117         &class_device_attr_lpfc_multi_ring_rctl,
1118         &class_device_attr_lpfc_multi_ring_type,
1119         &class_device_attr_lpfc_fdmi_on,
1120         &class_device_attr_lpfc_max_luns,
1121         &class_device_attr_nport_evt_cnt,
1122         &class_device_attr_management_version,
1123         &class_device_attr_board_mode,
1124         &class_device_attr_issue_reset,
1125         &class_device_attr_lpfc_poll,
1126         &class_device_attr_lpfc_poll_tmo,
1127         &class_device_attr_lpfc_use_msi,
1128         &class_device_attr_lpfc_soft_wwnn,
1129         &class_device_attr_lpfc_soft_wwpn,
1130         &class_device_attr_lpfc_soft_wwn_enable,
1131         NULL,
1132 };
1133
1134 static ssize_t
1135 sysfs_ctlreg_write(struct kobject *kobj, char *buf, loff_t off, size_t count)
1136 {
1137         size_t buf_off;
1138         struct Scsi_Host *host = class_to_shost(container_of(kobj,
1139                                              struct class_device, kobj));
1140         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
1141
1142         if ((off + count) > FF_REG_AREA_SIZE)
1143                 return -ERANGE;
1144
1145         if (count == 0) return 0;
1146
1147         if (off % 4 || count % 4 || (unsigned long)buf % 4)
1148                 return -EINVAL;
1149
1150         spin_lock_irq(phba->host->host_lock);
1151
1152         if (!(phba->fc_flag & FC_OFFLINE_MODE)) {
1153                 spin_unlock_irq(phba->host->host_lock);
1154                 return -EPERM;
1155         }
1156
1157         for (buf_off = 0; buf_off < count; buf_off += sizeof(uint32_t))
1158                 writel(*((uint32_t *)(buf + buf_off)),
1159                        phba->ctrl_regs_memmap_p + off + buf_off);
1160
1161         spin_unlock_irq(phba->host->host_lock);
1162
1163         return count;
1164 }
1165
1166 static ssize_t
1167 sysfs_ctlreg_read(struct kobject *kobj, char *buf, loff_t off, size_t count)
1168 {
1169         size_t buf_off;
1170         uint32_t * tmp_ptr;
1171         struct Scsi_Host *host = class_to_shost(container_of(kobj,
1172                                              struct class_device, kobj));
1173         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
1174
1175         if (off > FF_REG_AREA_SIZE)
1176                 return -ERANGE;
1177
1178         if ((off + count) > FF_REG_AREA_SIZE)
1179                 count = FF_REG_AREA_SIZE - off;
1180
1181         if (count == 0) return 0;
1182
1183         if (off % 4 || count % 4 || (unsigned long)buf % 4)
1184                 return -EINVAL;
1185
1186         spin_lock_irq(phba->host->host_lock);
1187
1188         for (buf_off = 0; buf_off < count; buf_off += sizeof(uint32_t)) {
1189                 tmp_ptr = (uint32_t *)(buf + buf_off);
1190                 *tmp_ptr = readl(phba->ctrl_regs_memmap_p + off + buf_off);
1191         }
1192
1193         spin_unlock_irq(phba->host->host_lock);
1194
1195         return count;
1196 }
1197
1198 static struct bin_attribute sysfs_ctlreg_attr = {
1199         .attr = {
1200                 .name = "ctlreg",
1201                 .mode = S_IRUSR | S_IWUSR,
1202                 .owner = THIS_MODULE,
1203         },
1204         .size = 256,
1205         .read = sysfs_ctlreg_read,
1206         .write = sysfs_ctlreg_write,
1207 };
1208
1209
1210 static void
1211 sysfs_mbox_idle (struct lpfc_hba * phba)
1212 {
1213         phba->sysfs_mbox.state = SMBOX_IDLE;
1214         phba->sysfs_mbox.offset = 0;
1215
1216         if (phba->sysfs_mbox.mbox) {
1217                 mempool_free(phba->sysfs_mbox.mbox,
1218                              phba->mbox_mem_pool);
1219                 phba->sysfs_mbox.mbox = NULL;
1220         }
1221 }
1222
1223 static ssize_t
1224 sysfs_mbox_write(struct kobject *kobj, char *buf, loff_t off, size_t count)
1225 {
1226         struct Scsi_Host * host =
1227                 class_to_shost(container_of(kobj, struct class_device, kobj));
1228         struct lpfc_hba * phba = (struct lpfc_hba*)host->hostdata;
1229         struct lpfcMboxq * mbox = NULL;
1230
1231         if ((count + off) > MAILBOX_CMD_SIZE)
1232                 return -ERANGE;
1233
1234         if (off % 4 ||  count % 4 || (unsigned long)buf % 4)
1235                 return -EINVAL;
1236
1237         if (count == 0)
1238                 return 0;
1239
1240         if (off == 0) {
1241                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1242                 if (!mbox)
1243                         return -ENOMEM;
1244                 memset(mbox, 0, sizeof (LPFC_MBOXQ_t));
1245         }
1246
1247         spin_lock_irq(host->host_lock);
1248
1249         if (off == 0) {
1250                 if (phba->sysfs_mbox.mbox)
1251                         mempool_free(mbox, phba->mbox_mem_pool);
1252                 else
1253                         phba->sysfs_mbox.mbox = mbox;
1254                 phba->sysfs_mbox.state = SMBOX_WRITING;
1255         } else {
1256                 if (phba->sysfs_mbox.state  != SMBOX_WRITING ||
1257                     phba->sysfs_mbox.offset != off           ||
1258                     phba->sysfs_mbox.mbox   == NULL ) {
1259                         sysfs_mbox_idle(phba);
1260                         spin_unlock_irq(host->host_lock);
1261                         return -EAGAIN;
1262                 }
1263         }
1264
1265         memcpy((uint8_t *) & phba->sysfs_mbox.mbox->mb + off,
1266                buf, count);
1267
1268         phba->sysfs_mbox.offset = off + count;
1269
1270         spin_unlock_irq(host->host_lock);
1271
1272         return count;
1273 }
1274
1275 static ssize_t
1276 sysfs_mbox_read(struct kobject *kobj, char *buf, loff_t off, size_t count)
1277 {
1278         struct Scsi_Host *host =
1279                 class_to_shost(container_of(kobj, struct class_device,
1280                                             kobj));
1281         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
1282         int rc;
1283
1284         if (off > MAILBOX_CMD_SIZE)
1285                 return -ERANGE;
1286
1287         if ((count + off) > MAILBOX_CMD_SIZE)
1288                 count = MAILBOX_CMD_SIZE - off;
1289
1290         if (off % 4 ||  count % 4 || (unsigned long)buf % 4)
1291                 return -EINVAL;
1292
1293         if (off && count == 0)
1294                 return 0;
1295
1296         spin_lock_irq(phba->host->host_lock);
1297
1298         if (off == 0 &&
1299             phba->sysfs_mbox.state  == SMBOX_WRITING &&
1300             phba->sysfs_mbox.offset >= 2 * sizeof(uint32_t)) {
1301
1302                 switch (phba->sysfs_mbox.mbox->mb.mbxCommand) {
1303                         /* Offline only */
1304                 case MBX_WRITE_NV:
1305                 case MBX_INIT_LINK:
1306                 case MBX_DOWN_LINK:
1307                 case MBX_CONFIG_LINK:
1308                 case MBX_CONFIG_RING:
1309                 case MBX_RESET_RING:
1310                 case MBX_UNREG_LOGIN:
1311                 case MBX_CLEAR_LA:
1312                 case MBX_DUMP_CONTEXT:
1313                 case MBX_RUN_DIAGS:
1314                 case MBX_RESTART:
1315                 case MBX_FLASH_WR_ULA:
1316                 case MBX_SET_MASK:
1317                 case MBX_SET_SLIM:
1318                 case MBX_SET_DEBUG:
1319                         if (!(phba->fc_flag & FC_OFFLINE_MODE)) {
1320                                 printk(KERN_WARNING "mbox_read:Command 0x%x "
1321                                        "is illegal in on-line state\n",
1322                                        phba->sysfs_mbox.mbox->mb.mbxCommand);
1323                                 sysfs_mbox_idle(phba);
1324                                 spin_unlock_irq(phba->host->host_lock);
1325                                 return -EPERM;
1326                         }
1327                 case MBX_LOAD_SM:
1328                 case MBX_READ_NV:
1329                 case MBX_READ_CONFIG:
1330                 case MBX_READ_RCONFIG:
1331                 case MBX_READ_STATUS:
1332                 case MBX_READ_XRI:
1333                 case MBX_READ_REV:
1334                 case MBX_READ_LNK_STAT:
1335                 case MBX_DUMP_MEMORY:
1336                 case MBX_DOWN_LOAD:
1337                 case MBX_UPDATE_CFG:
1338                 case MBX_KILL_BOARD:
1339                 case MBX_LOAD_AREA:
1340                 case MBX_LOAD_EXP_ROM:
1341                 case MBX_BEACON:
1342                 case MBX_DEL_LD_ENTRY:
1343                         break;
1344                 case MBX_READ_SPARM64:
1345                 case MBX_READ_LA:
1346                 case MBX_READ_LA64:
1347                 case MBX_REG_LOGIN:
1348                 case MBX_REG_LOGIN64:
1349                 case MBX_CONFIG_PORT:
1350                 case MBX_RUN_BIU_DIAG:
1351                         printk(KERN_WARNING "mbox_read: Illegal Command 0x%x\n",
1352                                phba->sysfs_mbox.mbox->mb.mbxCommand);
1353                         sysfs_mbox_idle(phba);
1354                         spin_unlock_irq(phba->host->host_lock);
1355                         return -EPERM;
1356                 default:
1357                         printk(KERN_WARNING "mbox_read: Unknown Command 0x%x\n",
1358                                phba->sysfs_mbox.mbox->mb.mbxCommand);
1359                         sysfs_mbox_idle(phba);
1360                         spin_unlock_irq(phba->host->host_lock);
1361                         return -EPERM;
1362                 }
1363
1364                 if (phba->fc_flag & FC_BLOCK_MGMT_IO) {
1365                         sysfs_mbox_idle(phba);
1366                         spin_unlock_irq(host->host_lock);
1367                         return  -EAGAIN;
1368                 }
1369
1370                 if ((phba->fc_flag & FC_OFFLINE_MODE) ||
1371                     (!(phba->sli.sli_flag & LPFC_SLI2_ACTIVE))){
1372
1373                         spin_unlock_irq(phba->host->host_lock);
1374                         rc = lpfc_sli_issue_mbox (phba,
1375                                                   phba->sysfs_mbox.mbox,
1376                                                   MBX_POLL);
1377                         spin_lock_irq(phba->host->host_lock);
1378
1379                 } else {
1380                         spin_unlock_irq(phba->host->host_lock);
1381                         rc = lpfc_sli_issue_mbox_wait (phba,
1382                                                        phba->sysfs_mbox.mbox,
1383                                 lpfc_mbox_tmo_val(phba,
1384                                     phba->sysfs_mbox.mbox->mb.mbxCommand) * HZ);
1385                         spin_lock_irq(phba->host->host_lock);
1386                 }
1387
1388                 if (rc != MBX_SUCCESS) {
1389                         if (rc == MBX_TIMEOUT) {
1390                                 phba->sysfs_mbox.mbox->mbox_cmpl =
1391                                         lpfc_sli_def_mbox_cmpl;
1392                                 phba->sysfs_mbox.mbox = NULL;
1393                         }
1394                         sysfs_mbox_idle(phba);
1395                         spin_unlock_irq(host->host_lock);
1396                         return  (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
1397                 }
1398                 phba->sysfs_mbox.state = SMBOX_READING;
1399         }
1400         else if (phba->sysfs_mbox.offset != off ||
1401                  phba->sysfs_mbox.state  != SMBOX_READING) {
1402                 printk(KERN_WARNING  "mbox_read: Bad State\n");
1403                 sysfs_mbox_idle(phba);
1404                 spin_unlock_irq(host->host_lock);
1405                 return -EAGAIN;
1406         }
1407
1408         memcpy(buf, (uint8_t *) & phba->sysfs_mbox.mbox->mb + off, count);
1409
1410         phba->sysfs_mbox.offset = off + count;
1411
1412         if (phba->sysfs_mbox.offset == MAILBOX_CMD_SIZE)
1413                 sysfs_mbox_idle(phba);
1414
1415         spin_unlock_irq(phba->host->host_lock);
1416
1417         return count;
1418 }
1419
1420 static struct bin_attribute sysfs_mbox_attr = {
1421         .attr = {
1422                 .name = "mbox",
1423                 .mode = S_IRUSR | S_IWUSR,
1424                 .owner = THIS_MODULE,
1425         },
1426         .size = MAILBOX_CMD_SIZE,
1427         .read = sysfs_mbox_read,
1428         .write = sysfs_mbox_write,
1429 };
1430
1431 int
1432 lpfc_alloc_sysfs_attr(struct lpfc_hba *phba)
1433 {
1434         struct Scsi_Host *host = phba->host;
1435         int error;
1436
1437         error = sysfs_create_bin_file(&host->shost_classdev.kobj,
1438                                                         &sysfs_ctlreg_attr);
1439         if (error)
1440                 goto out;
1441
1442         error = sysfs_create_bin_file(&host->shost_classdev.kobj,
1443                                                         &sysfs_mbox_attr);
1444         if (error)
1445                 goto out_remove_ctlreg_attr;
1446
1447         return 0;
1448 out_remove_ctlreg_attr:
1449         sysfs_remove_bin_file(&host->shost_classdev.kobj, &sysfs_ctlreg_attr);
1450 out:
1451         return error;
1452 }
1453
1454 void
1455 lpfc_free_sysfs_attr(struct lpfc_hba *phba)
1456 {
1457         struct Scsi_Host *host = phba->host;
1458
1459         sysfs_remove_bin_file(&host->shost_classdev.kobj, &sysfs_mbox_attr);
1460         sysfs_remove_bin_file(&host->shost_classdev.kobj, &sysfs_ctlreg_attr);
1461 }
1462
1463
1464 /*
1465  * Dynamic FC Host Attributes Support
1466  */
1467
1468 static void
1469 lpfc_get_host_port_id(struct Scsi_Host *shost)
1470 {
1471         struct lpfc_hba *phba = (struct lpfc_hba*)shost->hostdata;
1472         /* note: fc_myDID already in cpu endianness */
1473         fc_host_port_id(shost) = phba->fc_myDID;
1474 }
1475
1476 static void
1477 lpfc_get_host_port_type(struct Scsi_Host *shost)
1478 {
1479         struct lpfc_hba *phba = (struct lpfc_hba*)shost->hostdata;
1480
1481         spin_lock_irq(shost->host_lock);
1482
1483         if (phba->hba_state == LPFC_HBA_READY) {
1484                 if (phba->fc_topology == TOPOLOGY_LOOP) {
1485                         if (phba->fc_flag & FC_PUBLIC_LOOP)
1486                                 fc_host_port_type(shost) = FC_PORTTYPE_NLPORT;
1487                         else
1488                                 fc_host_port_type(shost) = FC_PORTTYPE_LPORT;
1489                 } else {
1490                         if (phba->fc_flag & FC_FABRIC)
1491                                 fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
1492                         else
1493                                 fc_host_port_type(shost) = FC_PORTTYPE_PTP;
1494                 }
1495         } else
1496                 fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
1497
1498         spin_unlock_irq(shost->host_lock);
1499 }
1500
1501 static void
1502 lpfc_get_host_port_state(struct Scsi_Host *shost)
1503 {
1504         struct lpfc_hba *phba = (struct lpfc_hba*)shost->hostdata;
1505
1506         spin_lock_irq(shost->host_lock);
1507
1508         if (phba->fc_flag & FC_OFFLINE_MODE)
1509                 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
1510         else {
1511                 switch (phba->hba_state) {
1512                 case LPFC_STATE_UNKNOWN:
1513                 case LPFC_WARM_START:
1514                 case LPFC_INIT_START:
1515                 case LPFC_INIT_MBX_CMDS:
1516                 case LPFC_LINK_DOWN:
1517                         fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
1518                         break;
1519                 case LPFC_LINK_UP:
1520                 case LPFC_LOCAL_CFG_LINK:
1521                 case LPFC_FLOGI:
1522                 case LPFC_FABRIC_CFG_LINK:
1523                 case LPFC_NS_REG:
1524                 case LPFC_NS_QRY:
1525                 case LPFC_BUILD_DISC_LIST:
1526                 case LPFC_DISC_AUTH:
1527                 case LPFC_CLEAR_LA:
1528                 case LPFC_HBA_READY:
1529                         /* Links up, beyond this port_type reports state */
1530                         fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
1531                         break;
1532                 case LPFC_HBA_ERROR:
1533                         fc_host_port_state(shost) = FC_PORTSTATE_ERROR;
1534                         break;
1535                 default:
1536                         fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
1537                         break;
1538                 }
1539         }
1540
1541         spin_unlock_irq(shost->host_lock);
1542 }
1543
1544 static void
1545 lpfc_get_host_speed(struct Scsi_Host *shost)
1546 {
1547         struct lpfc_hba *phba = (struct lpfc_hba*)shost->hostdata;
1548
1549         spin_lock_irq(shost->host_lock);
1550
1551         if (phba->hba_state == LPFC_HBA_READY) {
1552                 switch(phba->fc_linkspeed) {
1553                         case LA_1GHZ_LINK:
1554                                 fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
1555                         break;
1556                         case LA_2GHZ_LINK:
1557                                 fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
1558                         break;
1559                         case LA_4GHZ_LINK:
1560                                 fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
1561                         break;
1562                         default:
1563                                 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
1564                         break;
1565                 }
1566         }
1567
1568         spin_unlock_irq(shost->host_lock);
1569 }
1570
1571 static void
1572 lpfc_get_host_fabric_name (struct Scsi_Host *shost)
1573 {
1574         struct lpfc_hba *phba = (struct lpfc_hba*)shost->hostdata;
1575         u64 node_name;
1576
1577         spin_lock_irq(shost->host_lock);
1578
1579         if ((phba->fc_flag & FC_FABRIC) ||
1580             ((phba->fc_topology == TOPOLOGY_LOOP) &&
1581              (phba->fc_flag & FC_PUBLIC_LOOP)))
1582                 node_name = wwn_to_u64(phba->fc_fabparam.nodeName.u.wwn);
1583         else
1584                 /* fabric is local port if there is no F/FL_Port */
1585                 node_name = wwn_to_u64(phba->fc_nodename.u.wwn);
1586
1587         spin_unlock_irq(shost->host_lock);
1588
1589         fc_host_fabric_name(shost) = node_name;
1590 }
1591
1592 static void
1593 lpfc_get_host_symbolic_name (struct Scsi_Host *shost)
1594 {
1595         struct lpfc_hba *phba = (struct lpfc_hba*)shost->hostdata;
1596
1597         spin_lock_irq(shost->host_lock);
1598         lpfc_get_hba_sym_node_name(phba, fc_host_symbolic_name(shost));
1599         spin_unlock_irq(shost->host_lock);
1600 }
1601
1602 static struct fc_host_statistics *
1603 lpfc_get_stats(struct Scsi_Host *shost)
1604 {
1605         struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata;
1606         struct lpfc_sli *psli = &phba->sli;
1607         struct fc_host_statistics *hs = &phba->link_stats;
1608         struct lpfc_lnk_stat * lso = &psli->lnk_stat_offsets;
1609         LPFC_MBOXQ_t *pmboxq;
1610         MAILBOX_t *pmb;
1611         unsigned long seconds;
1612         int rc = 0;
1613
1614         if (phba->fc_flag & FC_BLOCK_MGMT_IO)
1615                 return NULL;
1616
1617         pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1618         if (!pmboxq)
1619                 return NULL;
1620         memset(pmboxq, 0, sizeof (LPFC_MBOXQ_t));
1621
1622         pmb = &pmboxq->mb;
1623         pmb->mbxCommand = MBX_READ_STATUS;
1624         pmb->mbxOwner = OWN_HOST;
1625         pmboxq->context1 = NULL;
1626
1627         if ((phba->fc_flag & FC_OFFLINE_MODE) ||
1628                 (!(psli->sli_flag & LPFC_SLI2_ACTIVE)))
1629                 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
1630         else
1631                 rc = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
1632
1633         if (rc != MBX_SUCCESS) {
1634                 if (rc == MBX_TIMEOUT)
1635                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1636                 else
1637                         mempool_free(pmboxq, phba->mbox_mem_pool);
1638                 return NULL;
1639         }
1640
1641         memset(hs, 0, sizeof (struct fc_host_statistics));
1642
1643         hs->tx_frames = pmb->un.varRdStatus.xmitFrameCnt;
1644         hs->tx_words = (pmb->un.varRdStatus.xmitByteCnt * 256);
1645         hs->rx_frames = pmb->un.varRdStatus.rcvFrameCnt;
1646         hs->rx_words = (pmb->un.varRdStatus.rcvByteCnt * 256);
1647
1648         memset(pmboxq, 0, sizeof (LPFC_MBOXQ_t));
1649         pmb->mbxCommand = MBX_READ_LNK_STAT;
1650         pmb->mbxOwner = OWN_HOST;
1651         pmboxq->context1 = NULL;
1652
1653         if ((phba->fc_flag & FC_OFFLINE_MODE) ||
1654             (!(psli->sli_flag & LPFC_SLI2_ACTIVE)))
1655                 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
1656         else
1657                 rc = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
1658
1659         if (rc != MBX_SUCCESS) {
1660                 if (rc == MBX_TIMEOUT)
1661                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1662                 else
1663                         mempool_free( pmboxq, phba->mbox_mem_pool);
1664                 return NULL;
1665         }
1666
1667         hs->link_failure_count = pmb->un.varRdLnk.linkFailureCnt;
1668         hs->loss_of_sync_count = pmb->un.varRdLnk.lossSyncCnt;
1669         hs->loss_of_signal_count = pmb->un.varRdLnk.lossSignalCnt;
1670         hs->prim_seq_protocol_err_count = pmb->un.varRdLnk.primSeqErrCnt;
1671         hs->invalid_tx_word_count = pmb->un.varRdLnk.invalidXmitWord;
1672         hs->invalid_crc_count = pmb->un.varRdLnk.crcCnt;
1673         hs->error_frames = pmb->un.varRdLnk.crcCnt;
1674
1675         hs->link_failure_count -= lso->link_failure_count;
1676         hs->loss_of_sync_count -= lso->loss_of_sync_count;
1677         hs->loss_of_signal_count -= lso->loss_of_signal_count;
1678         hs->prim_seq_protocol_err_count -= lso->prim_seq_protocol_err_count;
1679         hs->invalid_tx_word_count -= lso->invalid_tx_word_count;
1680         hs->invalid_crc_count -= lso->invalid_crc_count;
1681         hs->error_frames -= lso->error_frames;
1682
1683         if (phba->fc_topology == TOPOLOGY_LOOP) {
1684                 hs->lip_count = (phba->fc_eventTag >> 1);
1685                 hs->lip_count -= lso->link_events;
1686                 hs->nos_count = -1;
1687         } else {
1688                 hs->lip_count = -1;
1689                 hs->nos_count = (phba->fc_eventTag >> 1);
1690                 hs->nos_count -= lso->link_events;
1691         }
1692
1693         hs->dumped_frames = -1;
1694
1695         seconds = get_seconds();
1696         if (seconds < psli->stats_start)
1697                 hs->seconds_since_last_reset = seconds +
1698                                 ((unsigned long)-1 - psli->stats_start);
1699         else
1700                 hs->seconds_since_last_reset = seconds - psli->stats_start;
1701
1702         mempool_free(pmboxq, phba->mbox_mem_pool);
1703
1704         return hs;
1705 }
1706
1707 static void
1708 lpfc_reset_stats(struct Scsi_Host *shost)
1709 {
1710         struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata;
1711         struct lpfc_sli *psli = &phba->sli;
1712         struct lpfc_lnk_stat * lso = &psli->lnk_stat_offsets;
1713         LPFC_MBOXQ_t *pmboxq;
1714         MAILBOX_t *pmb;
1715         int rc = 0;
1716
1717         if (phba->fc_flag & FC_BLOCK_MGMT_IO)
1718                 return;
1719
1720         pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1721         if (!pmboxq)
1722                 return;
1723         memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1724
1725         pmb = &pmboxq->mb;
1726         pmb->mbxCommand = MBX_READ_STATUS;
1727         pmb->mbxOwner = OWN_HOST;
1728         pmb->un.varWords[0] = 0x1; /* reset request */
1729         pmboxq->context1 = NULL;
1730
1731         if ((phba->fc_flag & FC_OFFLINE_MODE) ||
1732                 (!(psli->sli_flag & LPFC_SLI2_ACTIVE)))
1733                 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
1734         else
1735                 rc = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
1736
1737         if (rc != MBX_SUCCESS) {
1738                 if (rc == MBX_TIMEOUT)
1739                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1740                 else
1741                         mempool_free(pmboxq, phba->mbox_mem_pool);
1742                 return;
1743         }
1744
1745         memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1746         pmb->mbxCommand = MBX_READ_LNK_STAT;
1747         pmb->mbxOwner = OWN_HOST;
1748         pmboxq->context1 = NULL;
1749
1750         if ((phba->fc_flag & FC_OFFLINE_MODE) ||
1751             (!(psli->sli_flag & LPFC_SLI2_ACTIVE)))
1752                 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
1753         else
1754                 rc = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
1755
1756         if (rc != MBX_SUCCESS) {
1757                 if (rc == MBX_TIMEOUT)
1758                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1759                 else
1760                         mempool_free( pmboxq, phba->mbox_mem_pool);
1761                 return;
1762         }
1763
1764         lso->link_failure_count = pmb->un.varRdLnk.linkFailureCnt;
1765         lso->loss_of_sync_count = pmb->un.varRdLnk.lossSyncCnt;
1766         lso->loss_of_signal_count = pmb->un.varRdLnk.lossSignalCnt;
1767         lso->prim_seq_protocol_err_count = pmb->un.varRdLnk.primSeqErrCnt;
1768         lso->invalid_tx_word_count = pmb->un.varRdLnk.invalidXmitWord;
1769         lso->invalid_crc_count = pmb->un.varRdLnk.crcCnt;
1770         lso->error_frames = pmb->un.varRdLnk.crcCnt;
1771         lso->link_events = (phba->fc_eventTag >> 1);
1772
1773         psli->stats_start = get_seconds();
1774
1775         mempool_free(pmboxq, phba->mbox_mem_pool);
1776
1777         return;
1778 }
1779
1780 /*
1781  * The LPFC driver treats linkdown handling as target loss events so there
1782  * are no sysfs handlers for link_down_tmo.
1783  */
1784 static void
1785 lpfc_get_starget_port_id(struct scsi_target *starget)
1786 {
1787         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1788         struct lpfc_hba *phba = (struct lpfc_hba *) shost->hostdata;
1789         uint32_t did = -1;
1790         struct lpfc_nodelist *ndlp = NULL;
1791
1792         spin_lock_irq(shost->host_lock);
1793         /* Search the mapped list for this target ID */
1794         list_for_each_entry(ndlp, &phba->fc_nlpmap_list, nlp_listp) {
1795                 if (starget->id == ndlp->nlp_sid) {
1796                         did = ndlp->nlp_DID;
1797                         break;
1798                 }
1799         }
1800         spin_unlock_irq(shost->host_lock);
1801
1802         fc_starget_port_id(starget) = did;
1803 }
1804
1805 static void
1806 lpfc_get_starget_node_name(struct scsi_target *starget)
1807 {
1808         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1809         struct lpfc_hba *phba = (struct lpfc_hba *) shost->hostdata;
1810         u64 node_name = 0;
1811         struct lpfc_nodelist *ndlp = NULL;
1812
1813         spin_lock_irq(shost->host_lock);
1814         /* Search the mapped list for this target ID */
1815         list_for_each_entry(ndlp, &phba->fc_nlpmap_list, nlp_listp) {
1816                 if (starget->id == ndlp->nlp_sid) {
1817                         node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
1818                         break;
1819                 }
1820         }
1821         spin_unlock_irq(shost->host_lock);
1822
1823         fc_starget_node_name(starget) = node_name;
1824 }
1825
1826 static void
1827 lpfc_get_starget_port_name(struct scsi_target *starget)
1828 {
1829         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1830         struct lpfc_hba *phba = (struct lpfc_hba *) shost->hostdata;
1831         u64 port_name = 0;
1832         struct lpfc_nodelist *ndlp = NULL;
1833
1834         spin_lock_irq(shost->host_lock);
1835         /* Search the mapped list for this target ID */
1836         list_for_each_entry(ndlp, &phba->fc_nlpmap_list, nlp_listp) {
1837                 if (starget->id == ndlp->nlp_sid) {
1838                         port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
1839                         break;
1840                 }
1841         }
1842         spin_unlock_irq(shost->host_lock);
1843
1844         fc_starget_port_name(starget) = port_name;
1845 }
1846
1847 static void
1848 lpfc_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
1849 {
1850         if (timeout)
1851                 rport->dev_loss_tmo = timeout;
1852         else
1853                 rport->dev_loss_tmo = 1;
1854 }
1855
1856
1857 #define lpfc_rport_show_function(field, format_string, sz, cast)        \
1858 static ssize_t                                                          \
1859 lpfc_show_rport_##field (struct class_device *cdev, char *buf)          \
1860 {                                                                       \
1861         struct fc_rport *rport = transport_class_to_rport(cdev);        \
1862         struct lpfc_rport_data *rdata = rport->hostdata;                \
1863         return snprintf(buf, sz, format_string,                         \
1864                 (rdata->target) ? cast rdata->target->field : 0);       \
1865 }
1866
1867 #define lpfc_rport_rd_attr(field, format_string, sz)                    \
1868         lpfc_rport_show_function(field, format_string, sz, )            \
1869 static FC_RPORT_ATTR(field, S_IRUGO, lpfc_show_rport_##field, NULL)
1870
1871
1872 struct fc_function_template lpfc_transport_functions = {
1873         /* fixed attributes the driver supports */
1874         .show_host_node_name = 1,
1875         .show_host_port_name = 1,
1876         .show_host_supported_classes = 1,
1877         .show_host_supported_fc4s = 1,
1878         .show_host_supported_speeds = 1,
1879         .show_host_maxframe_size = 1,
1880
1881         /* dynamic attributes the driver supports */
1882         .get_host_port_id = lpfc_get_host_port_id,
1883         .show_host_port_id = 1,
1884
1885         .get_host_port_type = lpfc_get_host_port_type,
1886         .show_host_port_type = 1,
1887
1888         .get_host_port_state = lpfc_get_host_port_state,
1889         .show_host_port_state = 1,
1890
1891         /* active_fc4s is shown but doesn't change (thus no get function) */
1892         .show_host_active_fc4s = 1,
1893
1894         .get_host_speed = lpfc_get_host_speed,
1895         .show_host_speed = 1,
1896
1897         .get_host_fabric_name = lpfc_get_host_fabric_name,
1898         .show_host_fabric_name = 1,
1899
1900         .get_host_symbolic_name = lpfc_get_host_symbolic_name,
1901         .show_host_symbolic_name = 1,
1902
1903         /*
1904          * The LPFC driver treats linkdown handling as target loss events
1905          * so there are no sysfs handlers for link_down_tmo.
1906          */
1907
1908         .get_fc_host_stats = lpfc_get_stats,
1909         .reset_fc_host_stats = lpfc_reset_stats,
1910
1911         .dd_fcrport_size = sizeof(struct lpfc_rport_data),
1912         .show_rport_maxframe_size = 1,
1913         .show_rport_supported_classes = 1,
1914
1915         .set_rport_dev_loss_tmo = lpfc_set_rport_loss_tmo,
1916         .show_rport_dev_loss_tmo = 1,
1917
1918         .get_starget_port_id  = lpfc_get_starget_port_id,
1919         .show_starget_port_id = 1,
1920
1921         .get_starget_node_name = lpfc_get_starget_node_name,
1922         .show_starget_node_name = 1,
1923
1924         .get_starget_port_name = lpfc_get_starget_port_name,
1925         .show_starget_port_name = 1,
1926
1927         .issue_fc_host_lip = lpfc_issue_lip,
1928         .dev_loss_tmo_callbk = lpfc_dev_loss_tmo_callbk,
1929         .terminate_rport_io = lpfc_terminate_rport_io,
1930 };
1931
1932 void
1933 lpfc_get_cfgparam(struct lpfc_hba *phba)
1934 {
1935         lpfc_log_verbose_init(phba, lpfc_log_verbose);
1936         lpfc_cr_delay_init(phba, lpfc_cr_delay);
1937         lpfc_cr_count_init(phba, lpfc_cr_count);
1938         lpfc_multi_ring_support_init(phba, lpfc_multi_ring_support);
1939         lpfc_multi_ring_rctl_init(phba, lpfc_multi_ring_rctl);
1940         lpfc_multi_ring_type_init(phba, lpfc_multi_ring_type);
1941         lpfc_lun_queue_depth_init(phba, lpfc_lun_queue_depth);
1942         lpfc_fcp_class_init(phba, lpfc_fcp_class);
1943         lpfc_use_adisc_init(phba, lpfc_use_adisc);
1944         lpfc_ack0_init(phba, lpfc_ack0);
1945         lpfc_topology_init(phba, lpfc_topology);
1946         lpfc_scan_down_init(phba, lpfc_scan_down);
1947         lpfc_link_speed_init(phba, lpfc_link_speed);
1948         lpfc_fdmi_on_init(phba, lpfc_fdmi_on);
1949         lpfc_discovery_threads_init(phba, lpfc_discovery_threads);
1950         lpfc_max_luns_init(phba, lpfc_max_luns);
1951         lpfc_poll_tmo_init(phba, lpfc_poll_tmo);
1952         lpfc_use_msi_init(phba, lpfc_use_msi);
1953         lpfc_devloss_tmo_init(phba, lpfc_devloss_tmo);
1954         lpfc_nodev_tmo_init(phba, lpfc_nodev_tmo);
1955         phba->cfg_poll = lpfc_poll;
1956         phba->cfg_soft_wwnn = 0L;
1957         phba->cfg_soft_wwpn = 0L;
1958
1959         /*
1960          * The total number of segments is the configuration value plus 2
1961          * since the IOCB need a command and response bde.
1962          */
1963         phba->cfg_sg_seg_cnt = LPFC_SG_SEG_CNT + 2;
1964
1965         /*
1966          * Since the sg_tablesize is module parameter, the sg_dma_buf_size
1967          * used to create the sg_dma_buf_pool must be dynamically calculated
1968          */
1969         phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
1970                         sizeof(struct fcp_rsp) +
1971                         (phba->cfg_sg_seg_cnt * sizeof(struct ulp_bde64));
1972
1973
1974         lpfc_hba_queue_depth_init(phba, lpfc_hba_queue_depth);
1975
1976         return;
1977 }