[SCSI] lpfc 8.1.7: Adding new issue_reset sysfs attribute
[pandora-kernel.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/pci.h>
24 #include <linux/interrupt.h>
25
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_device.h>
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_tcq.h>
30 #include <scsi/scsi_transport_fc.h>
31
32 #include "lpfc_hw.h"
33 #include "lpfc_sli.h"
34 #include "lpfc_disc.h"
35 #include "lpfc_scsi.h"
36 #include "lpfc.h"
37 #include "lpfc_logmsg.h"
38 #include "lpfc_version.h"
39 #include "lpfc_compat.h"
40 #include "lpfc_crtn.h"
41
42
43 static void
44 lpfc_jedec_to_ascii(int incr, char hdw[])
45 {
46         int i, j;
47         for (i = 0; i < 8; i++) {
48                 j = (incr & 0xf);
49                 if (j <= 9)
50                         hdw[7 - i] = 0x30 +  j;
51                  else
52                         hdw[7 - i] = 0x61 + j - 10;
53                 incr = (incr >> 4);
54         }
55         hdw[8] = 0;
56         return;
57 }
58
59 static ssize_t
60 lpfc_drvr_version_show(struct class_device *cdev, char *buf)
61 {
62         return snprintf(buf, PAGE_SIZE, LPFC_MODULE_DESC "\n");
63 }
64
65 static ssize_t
66 management_version_show(struct class_device *cdev, char *buf)
67 {
68         return snprintf(buf, PAGE_SIZE, DFC_API_VERSION "\n");
69 }
70
71 static ssize_t
72 lpfc_info_show(struct class_device *cdev, char *buf)
73 {
74         struct Scsi_Host *host = class_to_shost(cdev);
75         return snprintf(buf, PAGE_SIZE, "%s\n",lpfc_info(host));
76 }
77
78 static ssize_t
79 lpfc_serialnum_show(struct class_device *cdev, char *buf)
80 {
81         struct Scsi_Host *host = class_to_shost(cdev);
82         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
83         return snprintf(buf, PAGE_SIZE, "%s\n",phba->SerialNumber);
84 }
85
86 static ssize_t
87 lpfc_modeldesc_show(struct class_device *cdev, char *buf)
88 {
89         struct Scsi_Host *host = class_to_shost(cdev);
90         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
91         return snprintf(buf, PAGE_SIZE, "%s\n",phba->ModelDesc);
92 }
93
94 static ssize_t
95 lpfc_modelname_show(struct class_device *cdev, char *buf)
96 {
97         struct Scsi_Host *host = class_to_shost(cdev);
98         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
99         return snprintf(buf, PAGE_SIZE, "%s\n",phba->ModelName);
100 }
101
102 static ssize_t
103 lpfc_programtype_show(struct class_device *cdev, char *buf)
104 {
105         struct Scsi_Host *host = class_to_shost(cdev);
106         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
107         return snprintf(buf, PAGE_SIZE, "%s\n",phba->ProgramType);
108 }
109
110 static ssize_t
111 lpfc_portnum_show(struct class_device *cdev, char *buf)
112 {
113         struct Scsi_Host *host = class_to_shost(cdev);
114         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
115         return snprintf(buf, PAGE_SIZE, "%s\n",phba->Port);
116 }
117
118 static ssize_t
119 lpfc_fwrev_show(struct class_device *cdev, char *buf)
120 {
121         struct Scsi_Host *host = class_to_shost(cdev);
122         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
123         char fwrev[32];
124         lpfc_decode_firmware_rev(phba, fwrev, 1);
125         return snprintf(buf, PAGE_SIZE, "%s\n",fwrev);
126 }
127
128 static ssize_t
129 lpfc_hdw_show(struct class_device *cdev, char *buf)
130 {
131         char hdw[9];
132         struct Scsi_Host *host = class_to_shost(cdev);
133         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
134         lpfc_vpd_t *vp = &phba->vpd;
135         lpfc_jedec_to_ascii(vp->rev.biuRev, hdw);
136         return snprintf(buf, PAGE_SIZE, "%s\n", hdw);
137 }
138 static ssize_t
139 lpfc_option_rom_version_show(struct class_device *cdev, char *buf)
140 {
141         struct Scsi_Host *host = class_to_shost(cdev);
142         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
143         return snprintf(buf, PAGE_SIZE, "%s\n", phba->OptionROMVersion);
144 }
145 static ssize_t
146 lpfc_state_show(struct class_device *cdev, char *buf)
147 {
148         struct Scsi_Host *host = class_to_shost(cdev);
149         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
150         int len = 0;
151         switch (phba->hba_state) {
152         case LPFC_STATE_UNKNOWN:
153         case LPFC_WARM_START:
154         case LPFC_INIT_START:
155         case LPFC_INIT_MBX_CMDS:
156         case LPFC_LINK_DOWN:
157                 len += snprintf(buf + len, PAGE_SIZE-len, "Link Down\n");
158                 break;
159         case LPFC_LINK_UP:
160         case LPFC_LOCAL_CFG_LINK:
161                 len += snprintf(buf + len, PAGE_SIZE-len, "Link Up\n");
162                 break;
163         case LPFC_FLOGI:
164         case LPFC_FABRIC_CFG_LINK:
165         case LPFC_NS_REG:
166         case LPFC_NS_QRY:
167         case LPFC_BUILD_DISC_LIST:
168         case LPFC_DISC_AUTH:
169         case LPFC_CLEAR_LA:
170                 len += snprintf(buf + len, PAGE_SIZE-len,
171                                 "Link Up - Discovery\n");
172                 break;
173         case LPFC_HBA_READY:
174                 len += snprintf(buf + len, PAGE_SIZE-len,
175                                 "Link Up - Ready:\n");
176                 if (phba->fc_topology == TOPOLOGY_LOOP) {
177                         if (phba->fc_flag & FC_PUBLIC_LOOP)
178                                 len += snprintf(buf + len, PAGE_SIZE-len,
179                                                 "   Public Loop\n");
180                         else
181                                 len += snprintf(buf + len, PAGE_SIZE-len,
182                                                 "   Private Loop\n");
183                 } else {
184                         if (phba->fc_flag & FC_FABRIC)
185                                 len += snprintf(buf + len, PAGE_SIZE-len,
186                                                 "   Fabric\n");
187                         else
188                                 len += snprintf(buf + len, PAGE_SIZE-len,
189                                                 "   Point-2-Point\n");
190                 }
191         }
192         return len;
193 }
194
195 static ssize_t
196 lpfc_num_discovered_ports_show(struct class_device *cdev, char *buf)
197 {
198         struct Scsi_Host *host = class_to_shost(cdev);
199         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
200         return snprintf(buf, PAGE_SIZE, "%d\n", phba->fc_map_cnt +
201                                                         phba->fc_unmap_cnt);
202 }
203
204
205 static int
206 lpfc_issue_lip(struct Scsi_Host *host)
207 {
208         struct lpfc_hba *phba = (struct lpfc_hba *) host->hostdata;
209         LPFC_MBOXQ_t *pmboxq;
210         int mbxstatus = MBXERR_ERROR;
211
212         if ((phba->fc_flag & FC_OFFLINE_MODE) ||
213             (phba->hba_state != LPFC_HBA_READY))
214                 return -EPERM;
215
216         pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
217
218         if (!pmboxq)
219                 return -ENOMEM;
220
221         memset((void *)pmboxq, 0, sizeof (LPFC_MBOXQ_t));
222         pmboxq->mb.mbxCommand = MBX_DOWN_LINK;
223         pmboxq->mb.mbxOwner = OWN_HOST;
224
225         mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
226
227         if ((mbxstatus == MBX_SUCCESS) && (pmboxq->mb.mbxStatus == 0)) {
228                 memset((void *)pmboxq, 0, sizeof (LPFC_MBOXQ_t));
229                 lpfc_init_link(phba, pmboxq, phba->cfg_topology,
230                                phba->cfg_link_speed);
231                 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
232                                                      phba->fc_ratov * 2);
233         }
234
235         if (mbxstatus == MBX_TIMEOUT)
236                 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
237         else
238                 mempool_free(pmboxq, phba->mbox_mem_pool);
239
240         if (mbxstatus == MBXERR_ERROR)
241                 return -EIO;
242
243         return 0;
244 }
245
246 static int
247 lpfc_selective_reset(struct lpfc_hba *phba)
248 {
249         struct completion online_compl;
250         int status = 0;
251
252         init_completion(&online_compl);
253         lpfc_workq_post_event(phba, &status, &online_compl,
254                               LPFC_EVT_OFFLINE);
255         wait_for_completion(&online_compl);
256
257         if (status != 0)
258                 return -EIO;
259
260         init_completion(&online_compl);
261         lpfc_workq_post_event(phba, &status, &online_compl,
262                               LPFC_EVT_ONLINE);
263         wait_for_completion(&online_compl);
264
265         if (status != 0)
266                 return -EIO;
267
268         return 0;
269 }
270
271 static ssize_t
272 lpfc_issue_reset(struct class_device *cdev, const char *buf, size_t count)
273 {
274         struct Scsi_Host *host = class_to_shost(cdev);
275         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
276         int status = -EINVAL;
277
278         if (strncmp(buf, "selective", sizeof("selective") - 1) == 0)
279                 status = lpfc_selective_reset(phba);
280
281         if (status == 0)
282                 return strlen(buf);
283         else
284                 return status;
285 }
286
287 static ssize_t
288 lpfc_nport_evt_cnt_show(struct class_device *cdev, char *buf)
289 {
290         struct Scsi_Host *host = class_to_shost(cdev);
291         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
292         return snprintf(buf, PAGE_SIZE, "%d\n", phba->nport_event_cnt);
293 }
294
295 static ssize_t
296 lpfc_board_online_show(struct class_device *cdev, char *buf)
297 {
298         struct Scsi_Host *host = class_to_shost(cdev);
299         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
300
301         if (phba->fc_flag & FC_OFFLINE_MODE)
302                 return snprintf(buf, PAGE_SIZE, "0\n");
303         else
304                 return snprintf(buf, PAGE_SIZE, "1\n");
305 }
306
307 static ssize_t
308 lpfc_board_online_store(struct class_device *cdev, const char *buf,
309                                                                 size_t count)
310 {
311         struct Scsi_Host *host = class_to_shost(cdev);
312         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
313         struct completion online_compl;
314         int val=0, status=0;
315
316         if (sscanf(buf, "%d", &val) != 1)
317                 return -EINVAL;
318
319         init_completion(&online_compl);
320
321         if (val)
322                 lpfc_workq_post_event(phba, &status, &online_compl,
323                                                         LPFC_EVT_ONLINE);
324         else
325                 lpfc_workq_post_event(phba, &status, &online_compl,
326                                                         LPFC_EVT_OFFLINE);
327         wait_for_completion(&online_compl);
328         if (!status)
329                 return strlen(buf);
330         else
331                 return -EIO;
332 }
333
334 static ssize_t
335 lpfc_board_mode_show(struct class_device *cdev, char *buf)
336 {
337         struct Scsi_Host *host = class_to_shost(cdev);
338         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
339         char  * state;
340
341         if (phba->hba_state == LPFC_HBA_ERROR)
342                 state = "error";
343         else if (phba->hba_state == LPFC_WARM_START)
344                 state = "warm start";
345         else if (phba->hba_state == LPFC_INIT_START)
346                 state = "offline";
347         else
348                 state = "online";
349
350         return snprintf(buf, PAGE_SIZE, "%s\n", state);
351 }
352
353 static ssize_t
354 lpfc_board_mode_store(struct class_device *cdev, const char *buf, size_t count)
355 {
356         struct Scsi_Host *host = class_to_shost(cdev);
357         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
358         struct completion online_compl;
359         int status=0;
360
361         init_completion(&online_compl);
362
363         if(strncmp(buf, "online", sizeof("online") - 1) == 0)
364                 lpfc_workq_post_event(phba, &status, &online_compl,
365                                       LPFC_EVT_ONLINE);
366         else if (strncmp(buf, "offline", sizeof("offline") - 1) == 0)
367                 lpfc_workq_post_event(phba, &status, &online_compl,
368                                       LPFC_EVT_OFFLINE);
369         else if (strncmp(buf, "warm", sizeof("warm") - 1) == 0)
370                 lpfc_workq_post_event(phba, &status, &online_compl,
371                                       LPFC_EVT_WARM_START);
372         else if (strncmp(buf, "error", sizeof("error") - 1) == 0)
373                 lpfc_workq_post_event(phba, &status, &online_compl,
374                                       LPFC_EVT_KILL);
375         else
376                 return -EINVAL;
377
378         wait_for_completion(&online_compl);
379
380         if (!status)
381                 return strlen(buf);
382         else
383                 return -EIO;
384 }
385
386 static ssize_t
387 lpfc_poll_show(struct class_device *cdev, char *buf)
388 {
389         struct Scsi_Host *host = class_to_shost(cdev);
390         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
391
392         return snprintf(buf, PAGE_SIZE, "%#x\n", phba->cfg_poll);
393 }
394
395 static ssize_t
396 lpfc_poll_store(struct class_device *cdev, const char *buf,
397                 size_t count)
398 {
399         struct Scsi_Host *host = class_to_shost(cdev);
400         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
401         uint32_t creg_val;
402         uint32_t old_val;
403         int val=0;
404
405         if (!isdigit(buf[0]))
406                 return -EINVAL;
407
408         if (sscanf(buf, "%i", &val) != 1)
409                 return -EINVAL;
410
411         if ((val & 0x3) != val)
412                 return -EINVAL;
413
414         spin_lock_irq(phba->host->host_lock);
415
416         old_val = phba->cfg_poll;
417
418         if (val & ENABLE_FCP_RING_POLLING) {
419                 if ((val & DISABLE_FCP_RING_INT) &&
420                     !(old_val & DISABLE_FCP_RING_INT)) {
421                         creg_val = readl(phba->HCregaddr);
422                         creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
423                         writel(creg_val, phba->HCregaddr);
424                         readl(phba->HCregaddr); /* flush */
425
426                         lpfc_poll_start_timer(phba);
427                 }
428         } else if (val != 0x0) {
429                 spin_unlock_irq(phba->host->host_lock);
430                 return -EINVAL;
431         }
432
433         if (!(val & DISABLE_FCP_RING_INT) &&
434             (old_val & DISABLE_FCP_RING_INT))
435         {
436                 spin_unlock_irq(phba->host->host_lock);
437                 del_timer(&phba->fcp_poll_timer);
438                 spin_lock_irq(phba->host->host_lock);
439                 creg_val = readl(phba->HCregaddr);
440                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
441                 writel(creg_val, phba->HCregaddr);
442                 readl(phba->HCregaddr); /* flush */
443         }
444
445         phba->cfg_poll = val;
446
447         spin_unlock_irq(phba->host->host_lock);
448
449         return strlen(buf);
450 }
451
452 #define lpfc_param_show(attr)   \
453 static ssize_t \
454 lpfc_##attr##_show(struct class_device *cdev, char *buf) \
455 { \
456         struct Scsi_Host *host = class_to_shost(cdev);\
457         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;\
458         int val = 0;\
459         val = phba->cfg_##attr;\
460         return snprintf(buf, PAGE_SIZE, "%d\n",\
461                         phba->cfg_##attr);\
462 }
463
464 #define lpfc_param_hex_show(attr)       \
465 static ssize_t \
466 lpfc_##attr##_show(struct class_device *cdev, char *buf) \
467 { \
468         struct Scsi_Host *host = class_to_shost(cdev);\
469         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;\
470         int val = 0;\
471         val = phba->cfg_##attr;\
472         return snprintf(buf, PAGE_SIZE, "%#x\n",\
473                         phba->cfg_##attr);\
474 }
475
476 #define lpfc_param_init(attr, default, minval, maxval)  \
477 static int \
478 lpfc_##attr##_init(struct lpfc_hba *phba, int val) \
479 { \
480         if (val >= minval && val <= maxval) {\
481                 phba->cfg_##attr = val;\
482                 return 0;\
483         }\
484         lpfc_printf_log(phba, KERN_ERR, LOG_INIT, \
485                         "%d:0449 lpfc_"#attr" attribute cannot be set to %d, "\
486                         "allowed range is ["#minval", "#maxval"]\n", \
487                         phba->brd_no, val); \
488         phba->cfg_##attr = default;\
489         return -EINVAL;\
490 }
491
492 #define lpfc_param_set(attr, default, minval, maxval)   \
493 static int \
494 lpfc_##attr##_set(struct lpfc_hba *phba, int val) \
495 { \
496         if (val >= minval && val <= maxval) {\
497                 phba->cfg_##attr = val;\
498                 return 0;\
499         }\
500         lpfc_printf_log(phba, KERN_ERR, LOG_INIT, \
501                         "%d:0450 lpfc_"#attr" attribute cannot be set to %d, "\
502                         "allowed range is ["#minval", "#maxval"]\n", \
503                         phba->brd_no, val); \
504         return -EINVAL;\
505 }
506
507 #define lpfc_param_store(attr)  \
508 static ssize_t \
509 lpfc_##attr##_store(struct class_device *cdev, const char *buf, size_t count) \
510 { \
511         struct Scsi_Host *host = class_to_shost(cdev);\
512         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;\
513         int val=0;\
514         if (!isdigit(buf[0]))\
515                 return -EINVAL;\
516         if (sscanf(buf, "%i", &val) != 1)\
517                 return -EINVAL;\
518         if (lpfc_##attr##_set(phba, val) == 0) \
519                 return strlen(buf);\
520         else \
521                 return -EINVAL;\
522 }
523
524 #define LPFC_ATTR(name, defval, minval, maxval, desc) \
525 static int lpfc_##name = defval;\
526 module_param(lpfc_##name, int, 0);\
527 MODULE_PARM_DESC(lpfc_##name, desc);\
528 lpfc_param_init(name, defval, minval, maxval)
529
530 #define LPFC_ATTR_R(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_show(name)\
535 lpfc_param_init(name, defval, minval, maxval)\
536 static CLASS_DEVICE_ATTR(lpfc_##name, S_IRUGO , lpfc_##name##_show, NULL)
537
538 #define LPFC_ATTR_RW(name, defval, minval, maxval, desc) \
539 static int lpfc_##name = defval;\
540 module_param(lpfc_##name, int, 0);\
541 MODULE_PARM_DESC(lpfc_##name, desc);\
542 lpfc_param_show(name)\
543 lpfc_param_init(name, defval, minval, maxval)\
544 lpfc_param_set(name, defval, minval, maxval)\
545 lpfc_param_store(name)\
546 static CLASS_DEVICE_ATTR(lpfc_##name, S_IRUGO | S_IWUSR,\
547                          lpfc_##name##_show, lpfc_##name##_store)
548
549 #define LPFC_ATTR_HEX_R(name, defval, minval, maxval, desc) \
550 static int lpfc_##name = defval;\
551 module_param(lpfc_##name, int, 0);\
552 MODULE_PARM_DESC(lpfc_##name, desc);\
553 lpfc_param_hex_show(name)\
554 lpfc_param_init(name, defval, minval, maxval)\
555 static CLASS_DEVICE_ATTR(lpfc_##name, S_IRUGO , lpfc_##name##_show, NULL)
556
557 #define LPFC_ATTR_HEX_RW(name, defval, minval, maxval, desc) \
558 static int lpfc_##name = defval;\
559 module_param(lpfc_##name, int, 0);\
560 MODULE_PARM_DESC(lpfc_##name, desc);\
561 lpfc_param_hex_show(name)\
562 lpfc_param_init(name, defval, minval, maxval)\
563 lpfc_param_set(name, defval, minval, maxval)\
564 lpfc_param_store(name)\
565 static CLASS_DEVICE_ATTR(lpfc_##name, S_IRUGO | S_IWUSR,\
566                          lpfc_##name##_show, lpfc_##name##_store)
567
568 static CLASS_DEVICE_ATTR(info, S_IRUGO, lpfc_info_show, NULL);
569 static CLASS_DEVICE_ATTR(serialnum, S_IRUGO, lpfc_serialnum_show, NULL);
570 static CLASS_DEVICE_ATTR(modeldesc, S_IRUGO, lpfc_modeldesc_show, NULL);
571 static CLASS_DEVICE_ATTR(modelname, S_IRUGO, lpfc_modelname_show, NULL);
572 static CLASS_DEVICE_ATTR(programtype, S_IRUGO, lpfc_programtype_show, NULL);
573 static CLASS_DEVICE_ATTR(portnum, S_IRUGO, lpfc_portnum_show, NULL);
574 static CLASS_DEVICE_ATTR(fwrev, S_IRUGO, lpfc_fwrev_show, NULL);
575 static CLASS_DEVICE_ATTR(hdw, S_IRUGO, lpfc_hdw_show, NULL);
576 static CLASS_DEVICE_ATTR(state, S_IRUGO, lpfc_state_show, NULL);
577 static CLASS_DEVICE_ATTR(option_rom_version, S_IRUGO,
578                                         lpfc_option_rom_version_show, NULL);
579 static CLASS_DEVICE_ATTR(num_discovered_ports, S_IRUGO,
580                                         lpfc_num_discovered_ports_show, NULL);
581 static CLASS_DEVICE_ATTR(nport_evt_cnt, S_IRUGO, lpfc_nport_evt_cnt_show, NULL);
582 static CLASS_DEVICE_ATTR(lpfc_drvr_version, S_IRUGO, lpfc_drvr_version_show,
583                          NULL);
584 static CLASS_DEVICE_ATTR(management_version, S_IRUGO, management_version_show,
585                          NULL);
586 static CLASS_DEVICE_ATTR(board_online, S_IRUGO | S_IWUSR,
587                          lpfc_board_online_show, lpfc_board_online_store);
588 static CLASS_DEVICE_ATTR(board_mode, S_IRUGO | S_IWUSR,
589                          lpfc_board_mode_show, lpfc_board_mode_store);
590 static CLASS_DEVICE_ATTR(issue_reset, S_IWUSR, NULL, lpfc_issue_reset);
591
592 static int lpfc_poll = 0;
593 module_param(lpfc_poll, int, 0);
594 MODULE_PARM_DESC(lpfc_poll, "FCP ring polling mode control:"
595                  " 0 - none,"
596                  " 1 - poll with interrupts enabled"
597                  " 3 - poll and disable FCP ring interrupts");
598
599 static CLASS_DEVICE_ATTR(lpfc_poll, S_IRUGO | S_IWUSR,
600                          lpfc_poll_show, lpfc_poll_store);
601
602 /*
603 # lpfc_log_verbose: Only turn this flag on if you are willing to risk being
604 # deluged with LOTS of information.
605 # You can set a bit mask to record specific types of verbose messages:
606 #
607 # LOG_ELS                       0x1        ELS events
608 # LOG_DISCOVERY                 0x2        Link discovery events
609 # LOG_MBOX                      0x4        Mailbox events
610 # LOG_INIT                      0x8        Initialization events
611 # LOG_LINK_EVENT                0x10       Link events
612 # LOG_IP                        0x20       IP traffic history
613 # LOG_FCP                       0x40       FCP traffic history
614 # LOG_NODE                      0x80       Node table events
615 # LOG_MISC                      0x400      Miscellaneous events
616 # LOG_SLI                       0x800      SLI events
617 # LOG_CHK_COND                  0x1000     FCP Check condition flag
618 # LOG_LIBDFC                    0x2000     LIBDFC events
619 # LOG_ALL_MSG                   0xffff     LOG all messages
620 */
621 LPFC_ATTR_HEX_RW(log_verbose, 0x0, 0x0, 0xffff, "Verbose logging bit-mask");
622
623 /*
624 # lun_queue_depth:  This parameter is used to limit the number of outstanding
625 # commands per FCP LUN. Value range is [1,128]. Default value is 30.
626 */
627 LPFC_ATTR_R(lun_queue_depth, 30, 1, 128,
628             "Max number of FCP commands we can queue to a specific LUN");
629
630 /*
631 # hba_queue_depth:  This parameter is used to limit the number of outstanding
632 # commands per lpfc HBA. Value range is [32,8192]. If this parameter
633 # value is greater than the maximum number of exchanges supported by the HBA,
634 # then maximum number of exchanges supported by the HBA is used to determine
635 # the hba_queue_depth.
636 */
637 LPFC_ATTR_R(hba_queue_depth, 8192, 32, 8192,
638             "Max number of FCP commands we can queue to a lpfc HBA");
639
640 /*
641 # Some disk devices have a "select ID" or "select Target" capability.
642 # From a protocol standpoint "select ID" usually means select the
643 # Fibre channel "ALPA".  In the FC-AL Profile there is an "informative
644 # annex" which contains a table that maps a "select ID" (a number
645 # between 0 and 7F) to an ALPA.  By default, for compatibility with
646 # older drivers, the lpfc driver scans this table from low ALPA to high
647 # ALPA.
648 #
649 # Turning on the scan-down variable (on  = 1, off = 0) will
650 # cause the lpfc driver to use an inverted table, effectively
651 # scanning ALPAs from high to low. Value range is [0,1]. Default value is 1.
652 #
653 # (Note: This "select ID" functionality is a LOOP ONLY characteristic
654 # and will not work across a fabric. Also this parameter will take
655 # effect only in the case when ALPA map is not available.)
656 */
657 LPFC_ATTR_R(scan_down, 1, 0, 1,
658              "Start scanning for devices from highest ALPA to lowest");
659
660 /*
661 # lpfc_nodev_tmo: If set, it will hold all I/O errors on devices that disappear
662 # until the timer expires. Value range is [0,255]. Default value is 30.
663 # NOTE: this MUST be less then the SCSI Layer command timeout - 1.
664 */
665 LPFC_ATTR_RW(nodev_tmo, 30, 0, 255,
666              "Seconds driver will hold I/O waiting for a device to come back");
667
668 /*
669 # lpfc_topology:  link topology for init link
670 #            0x0  = attempt loop mode then point-to-point
671 #            0x01 = internal loopback mode
672 #            0x02 = attempt point-to-point mode only
673 #            0x04 = attempt loop mode only
674 #            0x06 = attempt point-to-point mode then loop
675 # Set point-to-point mode if you want to run as an N_Port.
676 # Set loop mode if you want to run as an NL_Port. Value range is [0,0x6].
677 # Default value is 0.
678 */
679 LPFC_ATTR_RW(topology, 0, 0, 6, "Select Fibre Channel topology");
680
681 /*
682 # lpfc_link_speed: Link speed selection for initializing the Fibre Channel
683 # connection.
684 #       0  = auto select (default)
685 #       1  = 1 Gigabaud
686 #       2  = 2 Gigabaud
687 #       4  = 4 Gigabaud
688 # Value range is [0,4]. Default value is 0.
689 */
690 LPFC_ATTR_R(link_speed, 0, 0, 4, "Select link speed");
691
692 /*
693 # lpfc_fcp_class:  Determines FC class to use for the FCP protocol.
694 # Value range is [2,3]. Default value is 3.
695 */
696 LPFC_ATTR_R(fcp_class, 3, 2, 3,
697              "Select Fibre Channel class of service for FCP sequences");
698
699 /*
700 # lpfc_use_adisc: Use ADISC for FCP rediscovery instead of PLOGI. Value range
701 # is [0,1]. Default value is 0.
702 */
703 LPFC_ATTR_RW(use_adisc, 0, 0, 1,
704              "Use ADISC on rediscovery to authenticate FCP devices");
705
706 /*
707 # lpfc_ack0: Use ACK0, instead of ACK1 for class 2 acknowledgement. Value
708 # range is [0,1]. Default value is 0.
709 */
710 LPFC_ATTR_R(ack0, 0, 0, 1, "Enable ACK0 support");
711
712 /*
713 # lpfc_cr_delay & lpfc_cr_count: Default values for I/O colaesing
714 # cr_delay (msec) or cr_count outstanding commands. cr_delay can take
715 # value [0,63]. cr_count can take value [0,255]. Default value of cr_delay
716 # is 0. Default value of cr_count is 1. The cr_count feature is disabled if
717 # cr_delay is set to 0.
718 */
719 LPFC_ATTR_RW(cr_delay, 0, 0, 63, "A count of milliseconds after which an "
720                 "interrupt response is generated");
721
722 LPFC_ATTR_RW(cr_count, 1, 1, 255, "A count of I/O completions after which an "
723                 "interrupt response is generated");
724
725 /*
726 # lpfc_multi_ring_support:  Determines how many rings to spread available
727 # cmd/rsp IOCB entries across.
728 # Value range is [1,2]. Default value is 1.
729 */
730 LPFC_ATTR_R(multi_ring_support, 1, 1, 2, "Determines number of primary "
731                 "SLI rings to spread IOCB entries across");
732
733 /*
734 # lpfc_fdmi_on: controls FDMI support.
735 #       0 = no FDMI support
736 #       1 = support FDMI without attribute of hostname
737 #       2 = support FDMI with attribute of hostname
738 # Value range [0,2]. Default value is 0.
739 */
740 LPFC_ATTR_RW(fdmi_on, 0, 0, 2, "Enable FDMI support");
741
742 /*
743 # Specifies the maximum number of ELS cmds we can have outstanding (for
744 # discovery). Value range is [1,64]. Default value = 32.
745 */
746 LPFC_ATTR(discovery_threads, 32, 1, 64, "Maximum number of ELS commands "
747                  "during discovery");
748
749 /*
750 # lpfc_max_luns: maximum number of LUNs per target driver will support
751 # Value range is [1,32768]. Default value is 256.
752 # NOTE: The SCSI layer will scan each target for this many luns
753 */
754 LPFC_ATTR_R(max_luns, 256, 1, 32768,
755              "Maximum number of LUNs per target driver will support");
756
757 /*
758 # lpfc_poll_tmo: .Milliseconds driver will wait between polling FCP ring.
759 # Value range is [1,255], default value is 10.
760 */
761 LPFC_ATTR_RW(poll_tmo, 10, 1, 255,
762              "Milliseconds driver will wait between polling FCP ring");
763
764 struct class_device_attribute *lpfc_host_attrs[] = {
765         &class_device_attr_info,
766         &class_device_attr_serialnum,
767         &class_device_attr_modeldesc,
768         &class_device_attr_modelname,
769         &class_device_attr_programtype,
770         &class_device_attr_portnum,
771         &class_device_attr_fwrev,
772         &class_device_attr_hdw,
773         &class_device_attr_option_rom_version,
774         &class_device_attr_state,
775         &class_device_attr_num_discovered_ports,
776         &class_device_attr_lpfc_drvr_version,
777         &class_device_attr_lpfc_log_verbose,
778         &class_device_attr_lpfc_lun_queue_depth,
779         &class_device_attr_lpfc_hba_queue_depth,
780         &class_device_attr_lpfc_nodev_tmo,
781         &class_device_attr_lpfc_fcp_class,
782         &class_device_attr_lpfc_use_adisc,
783         &class_device_attr_lpfc_ack0,
784         &class_device_attr_lpfc_topology,
785         &class_device_attr_lpfc_scan_down,
786         &class_device_attr_lpfc_link_speed,
787         &class_device_attr_lpfc_cr_delay,
788         &class_device_attr_lpfc_cr_count,
789         &class_device_attr_lpfc_multi_ring_support,
790         &class_device_attr_lpfc_fdmi_on,
791         &class_device_attr_lpfc_max_luns,
792         &class_device_attr_nport_evt_cnt,
793         &class_device_attr_management_version,
794         &class_device_attr_board_online,
795         &class_device_attr_board_mode,
796         &class_device_attr_issue_reset,
797         &class_device_attr_lpfc_poll,
798         &class_device_attr_lpfc_poll_tmo,
799         NULL,
800 };
801
802 static ssize_t
803 sysfs_ctlreg_write(struct kobject *kobj, char *buf, loff_t off, size_t count)
804 {
805         size_t buf_off;
806         struct Scsi_Host *host = class_to_shost(container_of(kobj,
807                                              struct class_device, kobj));
808         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
809
810         if ((off + count) > FF_REG_AREA_SIZE)
811                 return -ERANGE;
812
813         if (count == 0) return 0;
814
815         if (off % 4 || count % 4 || (unsigned long)buf % 4)
816                 return -EINVAL;
817
818         spin_lock_irq(phba->host->host_lock);
819
820         if (!(phba->fc_flag & FC_OFFLINE_MODE)) {
821                 spin_unlock_irq(phba->host->host_lock);
822                 return -EPERM;
823         }
824
825         for (buf_off = 0; buf_off < count; buf_off += sizeof(uint32_t))
826                 writel(*((uint32_t *)(buf + buf_off)),
827                        phba->ctrl_regs_memmap_p + off + buf_off);
828
829         spin_unlock_irq(phba->host->host_lock);
830
831         return count;
832 }
833
834 static ssize_t
835 sysfs_ctlreg_read(struct kobject *kobj, char *buf, loff_t off, size_t count)
836 {
837         size_t buf_off;
838         uint32_t * tmp_ptr;
839         struct Scsi_Host *host = class_to_shost(container_of(kobj,
840                                              struct class_device, kobj));
841         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
842
843         if (off > FF_REG_AREA_SIZE)
844                 return -ERANGE;
845
846         if ((off + count) > FF_REG_AREA_SIZE)
847                 count = FF_REG_AREA_SIZE - off;
848
849         if (count == 0) return 0;
850
851         if (off % 4 || count % 4 || (unsigned long)buf % 4)
852                 return -EINVAL;
853
854         spin_lock_irq(phba->host->host_lock);
855
856         for (buf_off = 0; buf_off < count; buf_off += sizeof(uint32_t)) {
857                 tmp_ptr = (uint32_t *)(buf + buf_off);
858                 *tmp_ptr = readl(phba->ctrl_regs_memmap_p + off + buf_off);
859         }
860
861         spin_unlock_irq(phba->host->host_lock);
862
863         return count;
864 }
865
866 static struct bin_attribute sysfs_ctlreg_attr = {
867         .attr = {
868                 .name = "ctlreg",
869                 .mode = S_IRUSR | S_IWUSR,
870                 .owner = THIS_MODULE,
871         },
872         .size = 256,
873         .read = sysfs_ctlreg_read,
874         .write = sysfs_ctlreg_write,
875 };
876
877
878 static void
879 sysfs_mbox_idle (struct lpfc_hba * phba)
880 {
881         phba->sysfs_mbox.state = SMBOX_IDLE;
882         phba->sysfs_mbox.offset = 0;
883
884         if (phba->sysfs_mbox.mbox) {
885                 mempool_free(phba->sysfs_mbox.mbox,
886                              phba->mbox_mem_pool);
887                 phba->sysfs_mbox.mbox = NULL;
888         }
889 }
890
891 static ssize_t
892 sysfs_mbox_write(struct kobject *kobj, char *buf, loff_t off, size_t count)
893 {
894         struct Scsi_Host * host =
895                 class_to_shost(container_of(kobj, struct class_device, kobj));
896         struct lpfc_hba * phba = (struct lpfc_hba*)host->hostdata;
897         struct lpfcMboxq * mbox = NULL;
898
899         if ((count + off) > MAILBOX_CMD_SIZE)
900                 return -ERANGE;
901
902         if (off % 4 ||  count % 4 || (unsigned long)buf % 4)
903                 return -EINVAL;
904
905         if (count == 0)
906                 return 0;
907
908         if (off == 0) {
909                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
910                 if (!mbox)
911                         return -ENOMEM;
912                 memset(mbox, 0, sizeof (LPFC_MBOXQ_t));
913         }
914
915         spin_lock_irq(host->host_lock);
916
917         if (off == 0) {
918                 if (phba->sysfs_mbox.mbox)
919                         mempool_free(mbox, phba->mbox_mem_pool);
920                 else
921                         phba->sysfs_mbox.mbox = mbox;
922                 phba->sysfs_mbox.state = SMBOX_WRITING;
923         } else {
924                 if (phba->sysfs_mbox.state  != SMBOX_WRITING ||
925                     phba->sysfs_mbox.offset != off           ||
926                     phba->sysfs_mbox.mbox   == NULL ) {
927                         sysfs_mbox_idle(phba);
928                         spin_unlock_irq(host->host_lock);
929                         return -EINVAL;
930                 }
931         }
932
933         memcpy((uint8_t *) & phba->sysfs_mbox.mbox->mb + off,
934                buf, count);
935
936         phba->sysfs_mbox.offset = off + count;
937
938         spin_unlock_irq(host->host_lock);
939
940         return count;
941 }
942
943 static ssize_t
944 sysfs_mbox_read(struct kobject *kobj, char *buf, loff_t off, size_t count)
945 {
946         struct Scsi_Host *host =
947                 class_to_shost(container_of(kobj, struct class_device,
948                                             kobj));
949         struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
950         int rc;
951
952         if (off > sizeof(MAILBOX_t))
953                 return -ERANGE;
954
955         if ((count + off) > sizeof(MAILBOX_t))
956                 count = sizeof(MAILBOX_t) - off;
957
958         if (off % 4 ||  count % 4 || (unsigned long)buf % 4)
959                 return -EINVAL;
960
961         if (off && count == 0)
962                 return 0;
963
964         spin_lock_irq(phba->host->host_lock);
965
966         if (off == 0 &&
967             phba->sysfs_mbox.state  == SMBOX_WRITING &&
968             phba->sysfs_mbox.offset >= 2 * sizeof(uint32_t)) {
969
970                 switch (phba->sysfs_mbox.mbox->mb.mbxCommand) {
971                         /* Offline only */
972                 case MBX_WRITE_NV:
973                 case MBX_INIT_LINK:
974                 case MBX_DOWN_LINK:
975                 case MBX_CONFIG_LINK:
976                 case MBX_CONFIG_RING:
977                 case MBX_RESET_RING:
978                 case MBX_UNREG_LOGIN:
979                 case MBX_CLEAR_LA:
980                 case MBX_DUMP_CONTEXT:
981                 case MBX_RUN_DIAGS:
982                 case MBX_RESTART:
983                 case MBX_FLASH_WR_ULA:
984                 case MBX_SET_MASK:
985                 case MBX_SET_SLIM:
986                 case MBX_SET_DEBUG:
987                         if (!(phba->fc_flag & FC_OFFLINE_MODE)) {
988                                 printk(KERN_WARNING "mbox_read:Command 0x%x "
989                                        "is illegal in on-line state\n",
990                                        phba->sysfs_mbox.mbox->mb.mbxCommand);
991                                 sysfs_mbox_idle(phba);
992                                 spin_unlock_irq(phba->host->host_lock);
993                                 return -EPERM;
994                         }
995                 case MBX_LOAD_SM:
996                 case MBX_READ_NV:
997                 case MBX_READ_CONFIG:
998                 case MBX_READ_RCONFIG:
999                 case MBX_READ_STATUS:
1000                 case MBX_READ_XRI:
1001                 case MBX_READ_REV:
1002                 case MBX_READ_LNK_STAT:
1003                 case MBX_DUMP_MEMORY:
1004                 case MBX_DOWN_LOAD:
1005                 case MBX_UPDATE_CFG:
1006                 case MBX_KILL_BOARD:
1007                 case MBX_LOAD_AREA:
1008                 case MBX_LOAD_EXP_ROM:
1009                 case MBX_BEACON:
1010                 case MBX_DEL_LD_ENTRY:
1011                         break;
1012                 case MBX_READ_SPARM64:
1013                 case MBX_READ_LA:
1014                 case MBX_READ_LA64:
1015                 case MBX_REG_LOGIN:
1016                 case MBX_REG_LOGIN64:
1017                 case MBX_CONFIG_PORT:
1018                 case MBX_RUN_BIU_DIAG:
1019                         printk(KERN_WARNING "mbox_read: Illegal Command 0x%x\n",
1020                                phba->sysfs_mbox.mbox->mb.mbxCommand);
1021                         sysfs_mbox_idle(phba);
1022                         spin_unlock_irq(phba->host->host_lock);
1023                         return -EPERM;
1024                 default:
1025                         printk(KERN_WARNING "mbox_read: Unknown Command 0x%x\n",
1026                                phba->sysfs_mbox.mbox->mb.mbxCommand);
1027                         sysfs_mbox_idle(phba);
1028                         spin_unlock_irq(phba->host->host_lock);
1029                         return -EPERM;
1030                 }
1031
1032                 if ((phba->fc_flag & FC_OFFLINE_MODE) ||
1033                     (!(phba->sli.sli_flag & LPFC_SLI2_ACTIVE))){
1034
1035                         spin_unlock_irq(phba->host->host_lock);
1036                         rc = lpfc_sli_issue_mbox (phba,
1037                                                   phba->sysfs_mbox.mbox,
1038                                                   MBX_POLL);
1039                         spin_lock_irq(phba->host->host_lock);
1040
1041                 } else {
1042                         spin_unlock_irq(phba->host->host_lock);
1043                         rc = lpfc_sli_issue_mbox_wait (phba,
1044                                                        phba->sysfs_mbox.mbox,
1045                                                        phba->fc_ratov * 2);
1046                         spin_lock_irq(phba->host->host_lock);
1047                 }
1048
1049                 if (rc != MBX_SUCCESS) {
1050                         sysfs_mbox_idle(phba);
1051                         spin_unlock_irq(host->host_lock);
1052                         return -ENODEV;
1053                 }
1054                 phba->sysfs_mbox.state = SMBOX_READING;
1055         }
1056         else if (phba->sysfs_mbox.offset != off ||
1057                  phba->sysfs_mbox.state  != SMBOX_READING) {
1058                 printk(KERN_WARNING  "mbox_read: Bad State\n");
1059                 sysfs_mbox_idle(phba);
1060                 spin_unlock_irq(host->host_lock);
1061                 return -EINVAL;
1062         }
1063
1064         memcpy(buf, (uint8_t *) & phba->sysfs_mbox.mbox->mb + off, count);
1065
1066         phba->sysfs_mbox.offset = off + count;
1067
1068         if (phba->sysfs_mbox.offset == sizeof(MAILBOX_t))
1069                 sysfs_mbox_idle(phba);
1070
1071         spin_unlock_irq(phba->host->host_lock);
1072
1073         return count;
1074 }
1075
1076 static struct bin_attribute sysfs_mbox_attr = {
1077         .attr = {
1078                 .name = "mbox",
1079                 .mode = S_IRUSR | S_IWUSR,
1080                 .owner = THIS_MODULE,
1081         },
1082         .size = sizeof(MAILBOX_t),
1083         .read = sysfs_mbox_read,
1084         .write = sysfs_mbox_write,
1085 };
1086
1087 int
1088 lpfc_alloc_sysfs_attr(struct lpfc_hba *phba)
1089 {
1090         struct Scsi_Host *host = phba->host;
1091         int error;
1092
1093         error = sysfs_create_bin_file(&host->shost_classdev.kobj,
1094                                                         &sysfs_ctlreg_attr);
1095         if (error)
1096                 goto out;
1097
1098         error = sysfs_create_bin_file(&host->shost_classdev.kobj,
1099                                                         &sysfs_mbox_attr);
1100         if (error)
1101                 goto out_remove_ctlreg_attr;
1102
1103         return 0;
1104 out_remove_ctlreg_attr:
1105         sysfs_remove_bin_file(&host->shost_classdev.kobj, &sysfs_ctlreg_attr);
1106 out:
1107         return error;
1108 }
1109
1110 void
1111 lpfc_free_sysfs_attr(struct lpfc_hba *phba)
1112 {
1113         struct Scsi_Host *host = phba->host;
1114
1115         sysfs_remove_bin_file(&host->shost_classdev.kobj, &sysfs_mbox_attr);
1116         sysfs_remove_bin_file(&host->shost_classdev.kobj, &sysfs_ctlreg_attr);
1117 }
1118
1119
1120 /*
1121  * Dynamic FC Host Attributes Support
1122  */
1123
1124 static void
1125 lpfc_get_host_port_id(struct Scsi_Host *shost)
1126 {
1127         struct lpfc_hba *phba = (struct lpfc_hba*)shost->hostdata;
1128         /* note: fc_myDID already in cpu endianness */
1129         fc_host_port_id(shost) = phba->fc_myDID;
1130 }
1131
1132 static void
1133 lpfc_get_host_port_type(struct Scsi_Host *shost)
1134 {
1135         struct lpfc_hba *phba = (struct lpfc_hba*)shost->hostdata;
1136
1137         spin_lock_irq(shost->host_lock);
1138
1139         if (phba->hba_state == LPFC_HBA_READY) {
1140                 if (phba->fc_topology == TOPOLOGY_LOOP) {
1141                         if (phba->fc_flag & FC_PUBLIC_LOOP)
1142                                 fc_host_port_type(shost) = FC_PORTTYPE_NLPORT;
1143                         else
1144                                 fc_host_port_type(shost) = FC_PORTTYPE_LPORT;
1145                 } else {
1146                         if (phba->fc_flag & FC_FABRIC)
1147                                 fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
1148                         else
1149                                 fc_host_port_type(shost) = FC_PORTTYPE_PTP;
1150                 }
1151         } else
1152                 fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
1153
1154         spin_unlock_irq(shost->host_lock);
1155 }
1156
1157 static void
1158 lpfc_get_host_port_state(struct Scsi_Host *shost)
1159 {
1160         struct lpfc_hba *phba = (struct lpfc_hba*)shost->hostdata;
1161
1162         spin_lock_irq(shost->host_lock);
1163
1164         if (phba->fc_flag & FC_OFFLINE_MODE)
1165                 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
1166         else {
1167                 switch (phba->hba_state) {
1168                 case LPFC_STATE_UNKNOWN:
1169                 case LPFC_WARM_START:
1170                 case LPFC_INIT_START:
1171                 case LPFC_INIT_MBX_CMDS:
1172                 case LPFC_LINK_DOWN:
1173                         fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
1174                         break;
1175                 case LPFC_LINK_UP:
1176                 case LPFC_LOCAL_CFG_LINK:
1177                 case LPFC_FLOGI:
1178                 case LPFC_FABRIC_CFG_LINK:
1179                 case LPFC_NS_REG:
1180                 case LPFC_NS_QRY:
1181                 case LPFC_BUILD_DISC_LIST:
1182                 case LPFC_DISC_AUTH:
1183                 case LPFC_CLEAR_LA:
1184                 case LPFC_HBA_READY:
1185                         /* Links up, beyond this port_type reports state */
1186                         fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
1187                         break;
1188                 case LPFC_HBA_ERROR:
1189                         fc_host_port_state(shost) = FC_PORTSTATE_ERROR;
1190                         break;
1191                 default:
1192                         fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
1193                         break;
1194                 }
1195         }
1196
1197         spin_unlock_irq(shost->host_lock);
1198 }
1199
1200 static void
1201 lpfc_get_host_speed(struct Scsi_Host *shost)
1202 {
1203         struct lpfc_hba *phba = (struct lpfc_hba*)shost->hostdata;
1204
1205         spin_lock_irq(shost->host_lock);
1206
1207         if (phba->hba_state == LPFC_HBA_READY) {
1208                 switch(phba->fc_linkspeed) {
1209                         case LA_1GHZ_LINK:
1210                                 fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
1211                         break;
1212                         case LA_2GHZ_LINK:
1213                                 fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
1214                         break;
1215                         case LA_4GHZ_LINK:
1216                                 fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
1217                         break;
1218                         default:
1219                                 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
1220                         break;
1221                 }
1222         }
1223
1224         spin_unlock_irq(shost->host_lock);
1225 }
1226
1227 static void
1228 lpfc_get_host_fabric_name (struct Scsi_Host *shost)
1229 {
1230         struct lpfc_hba *phba = (struct lpfc_hba*)shost->hostdata;
1231         u64 node_name;
1232
1233         spin_lock_irq(shost->host_lock);
1234
1235         if ((phba->fc_flag & FC_FABRIC) ||
1236             ((phba->fc_topology == TOPOLOGY_LOOP) &&
1237              (phba->fc_flag & FC_PUBLIC_LOOP)))
1238                 node_name = wwn_to_u64(phba->fc_fabparam.nodeName.u.wwn);
1239         else
1240                 /* fabric is local port if there is no F/FL_Port */
1241                 node_name = wwn_to_u64(phba->fc_nodename.u.wwn);
1242
1243         spin_unlock_irq(shost->host_lock);
1244
1245         fc_host_fabric_name(shost) = node_name;
1246 }
1247
1248
1249 static struct fc_host_statistics *
1250 lpfc_get_stats(struct Scsi_Host *shost)
1251 {
1252         struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata;
1253         struct lpfc_sli *psli = &phba->sli;
1254         struct fc_host_statistics *hs = &phba->link_stats;
1255         LPFC_MBOXQ_t *pmboxq;
1256         MAILBOX_t *pmb;
1257         int rc = 0;
1258
1259         pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1260         if (!pmboxq)
1261                 return NULL;
1262         memset(pmboxq, 0, sizeof (LPFC_MBOXQ_t));
1263
1264         pmb = &pmboxq->mb;
1265         pmb->mbxCommand = MBX_READ_STATUS;
1266         pmb->mbxOwner = OWN_HOST;
1267         pmboxq->context1 = NULL;
1268
1269         if ((phba->fc_flag & FC_OFFLINE_MODE) ||
1270                 (!(psli->sli_flag & LPFC_SLI2_ACTIVE)))
1271                 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
1272         else
1273                 rc = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
1274
1275         if (rc != MBX_SUCCESS) {
1276                 if (rc == MBX_TIMEOUT)
1277                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1278                 else
1279                         mempool_free(pmboxq, phba->mbox_mem_pool);
1280                 return NULL;
1281         }
1282
1283         memset(hs, 0, sizeof (struct fc_host_statistics));
1284
1285         hs->tx_frames = pmb->un.varRdStatus.xmitFrameCnt;
1286         hs->tx_words = (pmb->un.varRdStatus.xmitByteCnt * 256);
1287         hs->rx_frames = pmb->un.varRdStatus.rcvFrameCnt;
1288         hs->rx_words = (pmb->un.varRdStatus.rcvByteCnt * 256);
1289
1290         memset(pmboxq, 0, sizeof (LPFC_MBOXQ_t));
1291         pmb->mbxCommand = MBX_READ_LNK_STAT;
1292         pmb->mbxOwner = OWN_HOST;
1293         pmboxq->context1 = NULL;
1294
1295         if ((phba->fc_flag & FC_OFFLINE_MODE) ||
1296             (!(psli->sli_flag & LPFC_SLI2_ACTIVE)))
1297                 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
1298         else
1299                 rc = lpfc_sli_issue_mbox_wait(phba, pmboxq, phba->fc_ratov * 2);
1300
1301         if (rc != MBX_SUCCESS) {
1302                 if (rc == MBX_TIMEOUT)
1303                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1304                 else
1305                         mempool_free( pmboxq, phba->mbox_mem_pool);
1306                 return NULL;
1307         }
1308
1309         hs->link_failure_count = pmb->un.varRdLnk.linkFailureCnt;
1310         hs->loss_of_sync_count = pmb->un.varRdLnk.lossSyncCnt;
1311         hs->loss_of_signal_count = pmb->un.varRdLnk.lossSignalCnt;
1312         hs->prim_seq_protocol_err_count = pmb->un.varRdLnk.primSeqErrCnt;
1313         hs->invalid_tx_word_count = pmb->un.varRdLnk.invalidXmitWord;
1314         hs->invalid_crc_count = pmb->un.varRdLnk.crcCnt;
1315         hs->error_frames = pmb->un.varRdLnk.crcCnt;
1316
1317         if (phba->fc_topology == TOPOLOGY_LOOP) {
1318                 hs->lip_count = (phba->fc_eventTag >> 1);
1319                 hs->nos_count = -1;
1320         } else {
1321                 hs->lip_count = -1;
1322                 hs->nos_count = (phba->fc_eventTag >> 1);
1323         }
1324
1325         hs->dumped_frames = -1;
1326
1327 /* FIX ME */
1328         /*hs->SecondsSinceLastReset = (jiffies - lpfc_loadtime) / HZ;*/
1329
1330         return hs;
1331 }
1332
1333
1334 /*
1335  * The LPFC driver treats linkdown handling as target loss events so there
1336  * are no sysfs handlers for link_down_tmo.
1337  */
1338 static void
1339 lpfc_get_starget_port_id(struct scsi_target *starget)
1340 {
1341         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1342         struct lpfc_hba *phba = (struct lpfc_hba *) shost->hostdata;
1343         uint32_t did = -1;
1344         struct lpfc_nodelist *ndlp = NULL;
1345
1346         spin_lock_irq(shost->host_lock);
1347         /* Search the mapped list for this target ID */
1348         list_for_each_entry(ndlp, &phba->fc_nlpmap_list, nlp_listp) {
1349                 if (starget->id == ndlp->nlp_sid) {
1350                         did = ndlp->nlp_DID;
1351                         break;
1352                 }
1353         }
1354         spin_unlock_irq(shost->host_lock);
1355
1356         fc_starget_port_id(starget) = did;
1357 }
1358
1359 static void
1360 lpfc_get_starget_node_name(struct scsi_target *starget)
1361 {
1362         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1363         struct lpfc_hba *phba = (struct lpfc_hba *) shost->hostdata;
1364         u64 node_name = 0;
1365         struct lpfc_nodelist *ndlp = NULL;
1366
1367         spin_lock_irq(shost->host_lock);
1368         /* Search the mapped list for this target ID */
1369         list_for_each_entry(ndlp, &phba->fc_nlpmap_list, nlp_listp) {
1370                 if (starget->id == ndlp->nlp_sid) {
1371                         node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
1372                         break;
1373                 }
1374         }
1375         spin_unlock_irq(shost->host_lock);
1376
1377         fc_starget_node_name(starget) = node_name;
1378 }
1379
1380 static void
1381 lpfc_get_starget_port_name(struct scsi_target *starget)
1382 {
1383         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1384         struct lpfc_hba *phba = (struct lpfc_hba *) shost->hostdata;
1385         u64 port_name = 0;
1386         struct lpfc_nodelist *ndlp = NULL;
1387
1388         spin_lock_irq(shost->host_lock);
1389         /* Search the mapped list for this target ID */
1390         list_for_each_entry(ndlp, &phba->fc_nlpmap_list, nlp_listp) {
1391                 if (starget->id == ndlp->nlp_sid) {
1392                         port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
1393                         break;
1394                 }
1395         }
1396         spin_unlock_irq(shost->host_lock);
1397
1398         fc_starget_port_name(starget) = port_name;
1399 }
1400
1401 static void
1402 lpfc_get_rport_loss_tmo(struct fc_rport *rport)
1403 {
1404         /*
1405          * Return the driver's global value for device loss timeout plus
1406          * five seconds to allow the driver's nodev timer to run.
1407          */
1408         rport->dev_loss_tmo = lpfc_nodev_tmo + 5;
1409 }
1410
1411 static void
1412 lpfc_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
1413 {
1414         /*
1415          * The driver doesn't have a per-target timeout setting.  Set
1416          * this value globally. lpfc_nodev_tmo should be greater then 0.
1417          */
1418         if (timeout)
1419                 lpfc_nodev_tmo = timeout;
1420         else
1421                 lpfc_nodev_tmo = 1;
1422         rport->dev_loss_tmo = lpfc_nodev_tmo + 5;
1423 }
1424
1425
1426 #define lpfc_rport_show_function(field, format_string, sz, cast)        \
1427 static ssize_t                                                          \
1428 lpfc_show_rport_##field (struct class_device *cdev, char *buf)          \
1429 {                                                                       \
1430         struct fc_rport *rport = transport_class_to_rport(cdev);        \
1431         struct lpfc_rport_data *rdata = rport->hostdata;                \
1432         return snprintf(buf, sz, format_string,                         \
1433                 (rdata->target) ? cast rdata->target->field : 0);       \
1434 }
1435
1436 #define lpfc_rport_rd_attr(field, format_string, sz)                    \
1437         lpfc_rport_show_function(field, format_string, sz, )            \
1438 static FC_RPORT_ATTR(field, S_IRUGO, lpfc_show_rport_##field, NULL)
1439
1440
1441 struct fc_function_template lpfc_transport_functions = {
1442         /* fixed attributes the driver supports */
1443         .show_host_node_name = 1,
1444         .show_host_port_name = 1,
1445         .show_host_supported_classes = 1,
1446         .show_host_supported_fc4s = 1,
1447         .show_host_symbolic_name = 1,
1448         .show_host_supported_speeds = 1,
1449         .show_host_maxframe_size = 1,
1450
1451         /* dynamic attributes the driver supports */
1452         .get_host_port_id = lpfc_get_host_port_id,
1453         .show_host_port_id = 1,
1454
1455         .get_host_port_type = lpfc_get_host_port_type,
1456         .show_host_port_type = 1,
1457
1458         .get_host_port_state = lpfc_get_host_port_state,
1459         .show_host_port_state = 1,
1460
1461         /* active_fc4s is shown but doesn't change (thus no get function) */
1462         .show_host_active_fc4s = 1,
1463
1464         .get_host_speed = lpfc_get_host_speed,
1465         .show_host_speed = 1,
1466
1467         .get_host_fabric_name = lpfc_get_host_fabric_name,
1468         .show_host_fabric_name = 1,
1469
1470         /*
1471          * The LPFC driver treats linkdown handling as target loss events
1472          * so there are no sysfs handlers for link_down_tmo.
1473          */
1474
1475         .get_fc_host_stats = lpfc_get_stats,
1476
1477         /* the LPFC driver doesn't support resetting stats yet */
1478
1479         .dd_fcrport_size = sizeof(struct lpfc_rport_data),
1480         .show_rport_maxframe_size = 1,
1481         .show_rport_supported_classes = 1,
1482
1483         .get_rport_dev_loss_tmo = lpfc_get_rport_loss_tmo,
1484         .set_rport_dev_loss_tmo = lpfc_set_rport_loss_tmo,
1485         .show_rport_dev_loss_tmo = 1,
1486
1487         .get_starget_port_id  = lpfc_get_starget_port_id,
1488         .show_starget_port_id = 1,
1489
1490         .get_starget_node_name = lpfc_get_starget_node_name,
1491         .show_starget_node_name = 1,
1492
1493         .get_starget_port_name = lpfc_get_starget_port_name,
1494         .show_starget_port_name = 1,
1495
1496         .issue_fc_host_lip = lpfc_issue_lip,
1497 };
1498
1499 void
1500 lpfc_get_cfgparam(struct lpfc_hba *phba)
1501 {
1502         lpfc_log_verbose_init(phba, lpfc_log_verbose);
1503         lpfc_cr_delay_init(phba, lpfc_cr_delay);
1504         lpfc_cr_count_init(phba, lpfc_cr_count);
1505         lpfc_multi_ring_support_init(phba, lpfc_multi_ring_support);
1506         lpfc_lun_queue_depth_init(phba, lpfc_lun_queue_depth);
1507         lpfc_fcp_class_init(phba, lpfc_fcp_class);
1508         lpfc_use_adisc_init(phba, lpfc_use_adisc);
1509         lpfc_ack0_init(phba, lpfc_ack0);
1510         lpfc_topology_init(phba, lpfc_topology);
1511         lpfc_scan_down_init(phba, lpfc_scan_down);
1512         lpfc_nodev_tmo_init(phba, lpfc_nodev_tmo);
1513         lpfc_link_speed_init(phba, lpfc_link_speed);
1514         lpfc_fdmi_on_init(phba, lpfc_fdmi_on);
1515         lpfc_discovery_threads_init(phba, lpfc_discovery_threads);
1516         lpfc_max_luns_init(phba, lpfc_max_luns);
1517         lpfc_poll_tmo_init(phba, lpfc_poll_tmo);
1518
1519         phba->cfg_poll = lpfc_poll;
1520
1521         /*
1522          * The total number of segments is the configuration value plus 2
1523          * since the IOCB need a command and response bde.
1524          */
1525         phba->cfg_sg_seg_cnt = LPFC_SG_SEG_CNT + 2;
1526
1527         /*
1528          * Since the sg_tablesize is module parameter, the sg_dma_buf_size
1529          * used to create the sg_dma_buf_pool must be dynamically calculated
1530          */
1531         phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
1532                         sizeof(struct fcp_rsp) +
1533                         (phba->cfg_sg_seg_cnt * sizeof(struct ulp_bde64));
1534
1535         switch (phba->pcidev->device) {
1536         case PCI_DEVICE_ID_LP101:
1537         case PCI_DEVICE_ID_BSMB:
1538         case PCI_DEVICE_ID_ZSMB:
1539                 phba->cfg_hba_queue_depth = LPFC_LP101_HBA_Q_DEPTH;
1540                 break;
1541         case PCI_DEVICE_ID_RFLY:
1542         case PCI_DEVICE_ID_PFLY:
1543         case PCI_DEVICE_ID_BMID:
1544         case PCI_DEVICE_ID_ZMID:
1545         case PCI_DEVICE_ID_TFLY:
1546                 phba->cfg_hba_queue_depth = LPFC_LC_HBA_Q_DEPTH;
1547                 break;
1548         default:
1549                 phba->cfg_hba_queue_depth = LPFC_DFT_HBA_Q_DEPTH;
1550         }
1551
1552         if (phba->cfg_hba_queue_depth > lpfc_hba_queue_depth)
1553                 lpfc_hba_queue_depth_init(phba, lpfc_hba_queue_depth);
1554
1555         return;
1556 }