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